I was hoping someone could show how to properly append an array created with an sql query with data from a second query.
I have an array I created from a mysql resource which is correct.
while($row = mysql_fetch_array($result)){
$first_pass[] = $row;
}
But before I finish with the $first_pass array I want to do a second query, so inside the while loop before the $first_pass array I added;
$p = $row['productid'];
$gle = mysql_query("SELECT value FROM `extra_field_values` WHERE productid = $p' AND fieldid ='11' ");
$goog = mysql_fetch_row($gle);
$row['google_cat'] = $goog[0];
my question is, no matter how I add this query to the existing array, when I dump it, it does not look like just another index added to the array. I have tried the mysql_fetch_row with my own created index and I have tried using mysql_fetch_array but then it shows as another array in the array.
I think it will function fine the way it is but it does not look proper.
This is what the dump looks like:
array0
=>array
0 => string ‘3614’ (length=4)
‘variantid’ => string ‘3614’ (length=4)
1 => string ‘1406’ (length=4)
‘productid’ => string ‘1406’ (length=4)
2 => string ‘180-GL-QT-CAY-M’ (length=15)
‘productcode’ => string ‘180-GL-QT-CAY-M’ (length=15)
google_cat => ‘Clothing> Gloves’
where google_cat looks nothing like the rest of the array. So any input is appreciated.
Thanks
The difference is, your initial
$rowis populated usingmysql_fetch_array()which defaults to using theMYSQL_BOTHfetch method.This creates an array using both numeric indices and column name keys.
When you append your
'google_cat'key, you’re only creating an associative index.Unless you’re actually going to use the numeric indices, I’d recommend sticking to
mysql_fetch_assoc()instead ofmysql_fetch_array()Actually, what I really recommend is ditching the mysql extension all together and moving to PDO.