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) "email1@email.com" [1]=> string(23) "email2@email.com" [2]=> string(14) "email1@email.com" [3]=> string(23) "email2@email.com" [4]=> string(14) "email1@email.com" [5]=> string(23) "email2@email.com" } 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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.