Jump to content

[SOLVED] What's wrong with this code?


nasir1123

Recommended Posts

HI there,

I am trying to get all the info (emailaccounts) that is stored in a database table. To use them for sending an email.

I am doing something wrong though. :(

Please help me!

Thanks in advance.

 

<?

   $DBhost = "localhost";   
   $DBuser = "";         
   $DBpass = "";           
   $DBName = "";        
   $table = "2009_01_01";           
   $numComments = 10;      

$DBConn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Error in GuestBook Application: " . mysql_error());

mysql_select_db($DBName, $DBConn) or die("Error in GuestBook Application: " . mysql_error());


$query = mysql_query("SELECT email FROM `' . $table . '`");


$email_verzender = "[email protected]";
$onderwerp = "Test.";
$bericht = "Kijken of dit werkt, sorry voor het ongemak.";
$headers .= "Bcc: ".$email_verzender."\r\n"; 


while ($row = mysql_fetch_array($query)) {
mail($row[0], $onderwerp, $bericht, $headers);
}


?>

Link to comment
https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/
Share on other sites

what type of error are you getting?

$query = mysql_query("SELECT email FROM `' . $table . '`");

in this code you didnt close the string, it should look like this:

$query = mysql_query("SELECT email FROM `" . $table . "`");

 

This line...

 

$query = mysql_query("SELECT email FROM `' . $table . '`");

 

 

You enclose your query using double quotes, but then try using single quotes with your dot operator.. Either use one or the other, not both...

 

$query = mysql_query("SELECT email FROM `" . $table . "`;");

 

 

Also always check if your query returns a resource or an error!

Rather than writing your query and executing it all in one line, use a prepared statement so it can be echoed and put or die(mysql_error()) at the end of sql execution.

 

$sql = "SELECT email FROM `' . $table . '`";
echo $sql;
mysql_query($sql) or die (mysql_error());

 

Rather than writing your query and executing it all in one line, use a prepared statement so it can be echoed and put or die(mysql_error()) at the end of sql execution.

 

$sql = "SELECT email FROM `' . $table . '`";
echo $sql;
mysql_query($sql) or die (mysql_error());

 

That's not a prepared statement, by the way; that's just using a variable...

 

Try something like:

$sql = "SELECT email FROM $table";
$result = mysql_query($sql) or die(mysql_error());

With changing the $query code I managed to fix my problem ("  or ')

So many thanks.

 

I do have one more question though. Hopefully you can help me with that, or perhaps I need to start a new topic.

 

Some of the emailaccounts in the database are the same.

Is there a simple way to make write a statement that checks whether an emailaddress is being used more than once? So that that emailaddress will only be used in the $row once?

 

 

Lol I managed to fix it myself. I just need to add DISTINCT after SELECT in the $query.

 

Well I can close this topic now, but ill wait until tomorrow. Maybe someone has something useful to say :)

 

You should have still been getting a return.  DISTINCT will just grab all the unique emails...  So you are just trying to filter out duplicate emails?

@ Maq

 

My reason to start this topic was to find out what was wrong with my code.

With my code I just wanted to get all the unique emailaddresses from the database and use them for my mailsending.

But unfortunately I forgot that some people had registered their emailaddresses more than once, which resulted in sending multiple emails to the same person. To fix that I needed to use DISTINCT. My problems are solved.

(most of my problems are caused by the fact I don't know very much about PHP yet, still learning)

 

Thanks for all the replies.

@ Maq

 

My reason to start this topic was to find out what was wrong with my code.

With my code I just wanted to get all the unique emailaddresses from the database and use them for my mailsending.

But unfortunately I forgot that some people had registered their emailaddresses more than once, which resulted in sending multiple emails to the same person. To fix that I needed to use DISTINCT. My problems are solved.

(most of my problems are caused by the fact I don't know very much about PHP yet, still learning)

 

Thanks for all the replies.

 

Yes sorry, I just re-read through all the posts and realized what exactly happened. 

 

Glad everything is working properly.  :D

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.