RLJ Posted April 25, 2011 Share Posted April 25, 2011 Hi all, I have the following code that queries a MySQL database and outputs results on-screen. $result= mysql_query (" SELECT email,firstname,lastname FROM tablename WHERE ID IN('".$IDs."') "); if (!$result) {die('Error: Could not search database. ' );} while($row = mysql_fetch_assoc($result)) { echo $row['firstname']," ",$row['lastname'],"<br />"; $emails[]=$row['email']; } var_dump($emails); var_dump($IDs); However, the two var_dump's output the following (in the case of 2 results for the query): array(6) { [0]=> string(14) "[email protected]" [1]=> string(23) "[email protected]" [2]=> string(14) "[email protected]" [3]=> string(23) "[email protected]" [4]=> string(14) "[email protected]" [5]=> string(23) "[email protected]" } string(67) "9543219aa68ffa1d434dc8530f23fe48','d4384b2b493867706b0bcedda012ed48" ($IDs is an imploded array) Even though the MySQL table only contains one email per ID, both emails are listed 3 times in $emails, instead of just once. Can anyone see why this is? I'm stuck. Thanks! Link to comment https://forums.phpfreaks.com/topic/234689-array-contains-fieldvalue-from-mysql-query-3-times-instead-of-once/ Share on other sites More sharing options...
fugix Posted April 25, 2011 Share Posted April 25, 2011 hpw many keys/values do you have in your ids array? Link to comment https://forums.phpfreaks.com/topic/234689-array-contains-fieldvalue-from-mysql-query-3-times-instead-of-once/#findComment-1206038 Share on other sites More sharing options...
RLJ Posted April 25, 2011 Author Share Posted April 25, 2011 Just 2, they're also output via var_dump. Link to comment https://forums.phpfreaks.com/topic/234689-array-contains-fieldvalue-from-mysql-query-3-times-instead-of-once/#findComment-1206043 Share on other sites More sharing options...
PFMaBiSmAd Posted April 25, 2011 Share Posted April 25, 2011 Either you have some prior code on that page that is adding those entries to the $emails array or there are actually that amount of data in the database. How about initializing the $emails array right before the start of that block of code - $emails = array(); Link to comment https://forums.phpfreaks.com/topic/234689-array-contains-fieldvalue-from-mysql-query-3-times-instead-of-once/#findComment-1206046 Share on other sites More sharing options...
RLJ Posted April 25, 2011 Author Share Posted April 25, 2011 A yes, thanks. Stupid mistake, I was running the code several times and adding more and more to $emails. Cheers. Link to comment https://forums.phpfreaks.com/topic/234689-array-contains-fieldvalue-from-mysql-query-3-times-instead-of-once/#findComment-1206113 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.