Jump to content

array contains fieldvalue from MySQL query 3 times instead of once


RLJ

Recommended Posts

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!

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();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.