Goinfory Posted May 28, 2015 Share Posted May 28, 2015 Hi, i want to send email to my 600 clients through php email. but the loop works till 20 contacts, after that it show 500 internal server error. I am using hostforlife server. Here is the script: <?php for($x=1;$x<=600;$x++) { $con = mysqli_connect("host","database","password"); if (!$con){die('Could not connect: ' . mysqli_error($con));} mysqli_select_db($con,"database"); $sql="SELECT email FROM emails Where id = ".$x; $result = mysqli_query($con,$sql); $row = mysqli_fetch_array($result); $to = $row['email']; if ($to == "NULL") { } else { echo $row['email']; mysqli_close($con); // mail($to,"subject","message"); sleep(2); } } ?> Please help me, Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/ Share on other sites More sharing options...
QuickOldCar Posted May 28, 2015 Share Posted May 28, 2015 Forget the email for the moment, you are doing 600 connections and queries in the for loop. Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/#findComment-1512724 Share on other sites More sharing options...
CroNiX Posted May 28, 2015 Share Posted May 28, 2015 (edited) PHP has a default execution time of 300 seconds. Perhaps it's hitting that wall. Edited May 28, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/#findComment-1512765 Share on other sites More sharing options...
cyberRobot Posted May 29, 2015 Share Posted May 29, 2015 You could try something like this: <?php //CONNECT TO DATABASE $con = mysqli_connect("host","database","password"); if (!$con){die('Could not connect: ' . mysqli_error($con));} mysqli_select_db($con,"database"); //GET EMAILS $sql = "SELECT email FROM emails WHERE id <= 600 AND email IS NOT NULL"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_assoc($result)) { echo $row['email']; } //CLOSE DATABASE mysqli_close($con); ?> Note: if you're not doing so already, you'll want to look into storing you're database connection script in an external file and use something like require_once() to import it. That way when you want / need to change the database login credentials, you'll only need to change it once. 1 Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/#findComment-1512803 Share on other sites More sharing options...
gizmola Posted May 29, 2015 Share Posted May 29, 2015 What is likely happening is, as was pointed out, you are trying to make 600 database connections. On your host, this is limited and you are running out of resources and memory at 20, and then you get the 500 error. cyberRobot's post should fix your problem and is the standard way to have code like this. You should also try and work in 2 parameters: start= count= This will allow you to have this script be used to email any batch of your database. This is the type of script you would want to run from a shell, but I'm guessing you don't have shell access to your host. Taking Cyber's example and expanding on it: <?php // Get params if (isset($_GET['start'])) { $start = (int)$_GET['start']; } else { $start = 0; } if (isset($_GET['count'])) { $count = (int)$_GET['count']; } else { //default to 600 rows $count = 600; } $count += $start; //CONNECT TO DATABASE $con = mysqli_connect("host","database","password"); if (!$con){die('Could not connect: ' . mysqli_error($con));} mysqli_select_db($con,"database"); //GET EMAILS $sql = "SELECT email FROM emails WHERE id BETWEEN $start AND $count AND email IS NOT NULL"; $result = mysqli_query($con,$sql); while($row = mysqli_fetch_assoc($result)) { echo $row['email']; // Send email here } //CLOSE DATABASE mysqli_close($con); Now you could call your script as: email.php?start=100&count=50 Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/#findComment-1512831 Share on other sites More sharing options...
Barand Posted May 29, 2015 Share Posted May 29, 2015 I'm sure you meant id BETWEEN $start AND $start+$count-1 Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/#findComment-1512840 Share on other sites More sharing options...
gizmola Posted May 30, 2015 Share Posted May 30, 2015 I'm sure you meant id BETWEEN $start AND $start+$count-1 The -1 looks better, however, I actually had $count += $start in the code. Hopefully the OP gets the idea. Quote Link to comment https://forums.phpfreaks.com/topic/296512-getting-500-internet-server-error-while-using-php-email/#findComment-1512841 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.