2017-02-09 58 views
0

我必須在運行MySQL查詢像這樣PHP等待MYSQL完成查詢

$date_start = '2017-01-01'; 
$date_end = '2017-01-10'; 
while ($date_start < $date_end){ 
    $sql ("SELECT id FROM mytable WHERE mydate = '$data_start'"); [<--edit] 
    $result = $db->query($sql); 
    $f = $result->fetch(); 
    echo 'result is : '.$f['id']; 
    $date_start = date('Y-m-d', strtotime($date_start. ' + 1 days')); 
} 

ID結果迴路總是與第一$ DATE_START,那麼首先SQL查詢...

如何等待MySQL的MySQL完成查詢?

編輯:

好了,有我在這裏創建循環,而是分心一個錯誤!這不是一個複製/粘貼

在此之後,我寫

ID result was always related to first $date_start, then first sql query... 

這意味着這不是一個代碼問題,循環工作,否則我有一個PHP的錯誤!

我將與MySQL錯誤報告

+0

此代碼沒有意義。 – apokryfos

+0

你的代碼全是紅色的。你怎麼看? –

+0

只需查看突出顯示的顏色即可。在這段代碼中有一個明顯的字符串文字問題 – RiggsFolly

回答

3

嘗試你有一個字符串字面沒有正確關閉,查詢到的變量$sql的分配不正確。因此腳本崩潰了

// while testing specially if you are developing on a live server 
// that will have error reporting to the screen turned off 
// add these 4 lines so you see your errors on the browser page 
ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); 

$date_start = '2017-01-01'; 
$date_end = '2017-01-10'; 
while ($date_start < $date_end){ 
    $sql = "SELECT id FROM mytable WHERE mydate = '$data_start'"; 
    // --new character added to close the literal------------->^ 
    //() removed 
    $result = $db->query($sql); 
    // check for errors 
    if (!$result) { 
     echo $result->error; 
     exit; 
    } 
    $f = $result->fetch(); 
    echo 'result is : '.$f['id']; 
    $date_start = date('Y-m-d', strtotime($date_start. ' + 1 days')); 
} 
+0

謝謝,我會嘗試使用錯誤報告,特別是我從未使用過的MYSQL – FireFoxII