Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving a JOIN array from MySql
    text
    copied!<p>I'm pulling a joined query from the DB using the following code:</p> <pre><code>$query = 'SELECT `profiles`.`city`, `users`.`first_name`, `users`.`last_name`' . ' FROM profiles, users' . ' WHERE ((`profiles`.`user_id` = 3) AND (`users`.`user_id` = 3))' . ' LIMIT 1'; $result = $connection -&gt; query($query); $arr = $result -&gt; fetch_array(); $feedback = $arr; $result -&gt; close(); return $feedback; </code></pre> <p>This isn't my final code, this is just preliminary to test my join.</p> <p>The query seems to work fine, it pulls the single row of information I need but I noticed that when I <code>var_dump</code> it, it's put the array into 2 different indexes, a numerical index and an index of the name of the db field. Here's what the <code>var_dump</code> looks like:</p> <pre><code>array(6) { [0]=&gt; string(4) "Reno" ["city"]=&gt; string(4) "Reno" [1]=&gt; string(4) "John" ["first_name"]=&gt; string(4) "John" [2]=&gt; string(3) "Doe" ["last_name"]=&gt; string(3) "Doe" } </code></pre> <p>This is effectively doubling the size of my query result. For a single row this won't be that big of a deal but if/when I begin to use this same method to draw multiple records, doubling the size of the result can be costly.</p> <p>I've tried to do a <code>foreach</code> loop:</p> <pre><code>foreach ($arr as $key=&gt;$value) { $feedback[$key] = $value; } </code></pre> <p>but it leaves me with the same result. Am I doing something wrong and if so, how do I correct it or is this normal?</p> <p>Thanks for you help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload