PHP Sleeping Infinite While Loop -
what doing wrong here? missing how while loops work in php? want keep looping in background on server after page closed.
important note: doesn't work if keep page open.
i feel theres obvious syntactical error i'm making php.
ignore_user_abort(true); set_time_limit(0); while(1){ $dbconnection = mysqli_connect($servername, $username, $password, $dbname); $query = "select user_id users email = '$user_email'"; $id = mysqli_query($dbconnection, $query); $idrow = mysqli_fetch_assoc($id); $result = $idrow['user_id']; $querydata1 = "select data1 queue user_id = '$result'"; $data1 = mysqli_query($dbconnection, $querydata1); $data1row = mysqli_fetch_assoc($data1); $resultdata1 = $data1row['data1']; if($resultdata1 != null){ $querydata2 = "select data2 queue user_id = '$result'"; $data2 = mysqli_query($dbconnection, $querydata2); $data2row = mysqli_fetch_assoc($data2); $resultdata2 = $data2row['data2']; include 'phpmailerautoload.php'; $mail = new phpmailer; $mail->issmtp(); $mail->host = 'smtp.mandrillapp.com'; $mail->smtpauth = true; $mail->username = 'username'; $mail->password = 'password'; $mail->smtpsecure = 'tls'; $mail->port = 587; $mail->from = 'test@test.com'; $mail->fromname = 'test'; $mail->addaddress($user_email); $mail->subject = 'test'; $mail->body = "$resultdata2"; $mail->send(); } else { break; } time_sleep_until(microtime(true)+10); }
hey guys, i'm again!
so, want code is:
- take user input
- query database using user input
- email results every 60 seconds.
my solution put last 2 stages in while loop , making script sleep 60 seconds @ end.
the result: 1 email, useful data.
most servers have time limit execution php scripts. in cases script run 30 seconds before timing out. if have access to server configuration can change infinite.
set_time_limit(0)
doesn't work depending on server configuration. while tried same thing , nothing, changing settings in server did me.
edit
due complains changing default settings on server create security risk can different. looking @ comments want query data , email said mention data.
you can creating stand alone php script (not while loop) follows logic 1 time. using jquery can call script many times , want when webpage loaded.
to make script run on background without user interaction want create cronjob calls script every (you can define time interval).
Comments
Post a Comment