nasir1123 Posted January 21, 2009 Share Posted January 21, 2009 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 = "myemail@gmail.com"; $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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/ Share on other sites More sharing options...
jjacquay712 Posted January 21, 2009 Share Posted January 21, 2009 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 . "`"); Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742571 Share on other sites More sharing options...
printf Posted January 21, 2009 Share Posted January 21, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742576 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742580 Share on other sites More sharing options...
nasir1123 Posted January 21, 2009 Author Share Posted January 21, 2009 This is the error I get: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /url/file.php on line 32 Btw thanks for all the answers. Appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742664 Share on other sites More sharing options...
Maq Posted January 21, 2009 Share Posted January 21, 2009 Where's line 32? Your code only goes to 29... Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742672 Share on other sites More sharing options...
DarkWater Posted January 21, 2009 Share Posted January 21, 2009 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()); Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742673 Share on other sites More sharing options...
nasir1123 Posted January 21, 2009 Author Share Posted January 21, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742678 Share on other sites More sharing options...
nasir1123 Posted January 21, 2009 Author Share Posted January 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-742733 Share on other sites More sharing options...
Maq Posted January 22, 2009 Share Posted January 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-743208 Share on other sites More sharing options...
nasir1123 Posted January 23, 2009 Author Share Posted January 23, 2009 @ 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. Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-744185 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 @ 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. Quote Link to comment https://forums.phpfreaks.com/topic/141829-solved-whats-wrong-with-this-code/#findComment-744223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.