php - array merge only showing one value -
i have these 2 queries
mysql_select_db($database_dbconnect, $dbconnect); $query_asset = "select idasset, assetname asset idlocation = '1'"; $assetup = mysql_query($query_asset, $dbconnect) or die(mysql_error()); $array1 = mysql_fetch_assoc($assetup); mysql_select_db($database_dbconnect, $dbconnect); $query_docup = "select assetdocstand.idasset, asset.assetname assetdocstand inner join asset on assetdocstand.idasset = asset.idasset assetdocstand.iddocumentstandards = '2' , asset.idlocation ='1'"; $docup = mysql_query($query_docup, $dbconnect) or die(mysql_error()); $array2 = mysql_fetch_assoc($docup);
one gets me assets in current location. other gets me assets in current location have document uploaded standard of 2. please note have took out error checking purposes of post.
if print results so:
do{ print_r($array1); echo "<br>"; }while($array1 = mysql_fetch_assoc($assetup)); echo "<br><br><br>"; do{ print_r($array2); echo "<br>"; }while($array2 = mysql_fetch_assoc($docup)); echo "<br><br><br>";
i following.
array ( [idasset] => 10000005 [assetname] => hp ) array ( [idasset] => 10000006 [assetname] => hp server ) array ( [idasset] => 10000009 [assetname] => hp laptop ) array ( [idasset] => 10000010 [assetname] => office printer ) array ( [idasset] => 10000023 [assetname] => test ) array ( [idasset] => 10000023 [assetname] => test )
no have been trying output assets have no document uploaded. can see above first 4 assets first list. next thing going merge arrays , use array_unique on new array when this;
$array3= array_merge($array1, $array2); print_r($array3);
all see following;
idasset; 10000023 assetname; test
i don't understand other 4 assets have gone? can me see mistake? guys , gals
first of all, not use mysql_*
since function deprecated , stop being supported. use mysqli_* , pdo instead.
that said, using, , understanding mysql_fetch_assoc
wrong. example mind pointed in right direction this:
<? $q = mysql_query("..."); while ($f = mysql_fetch_assoc($q)) { //do something, irrelevant } print_r($f); ?>
this print out the last row of query result. because mysql_fetch_assoc
fetches row, returns it, , moves pointer next row. when loop finishes, have last row in variable. did not realize this, because print_r
inside loop, should move outside of loop, realize see last row.
example work this:
<? $q1 = mysql_query(""); $array1 = array(); while ($f = mysql_fetch_assoc($q1)) $array1[] = $f; $q2 = mysql_query(""); $array2 = array(); while ($f = mysql_fetch_assoc($q2)) $array2[] = $f; ?>
if print_r($array1);
, print_r($array2);
after these 2 loops, see $array1
, $array2
have full resultsets. need merge them afterwards, , should trick.
let me know if works you.
Comments
Post a Comment