dongxian1921 2017-02-09 21:49
浏览 87
已采纳

PHP等待MYSQL完成查询

I have to run mysql query in a for loop like this

$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 result was always related to first $date_start, then first sql query...

How to wait php for mysql to finish query?

EDIT.:

Ok, there was a mistake when I create the loop here, but is a distraction! It's not a copy/paste

After this I wrote

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

This means that is not a code problem, the loop works, else I have a php error!

I will try with MYSQL error reporting

  • 写回答

1条回答 默认 最新

  • doupingmao1903 2017-02-09 22:06
    关注

    You have a string literal that is not closed properly, and the assignment of the query to the variable $sql is incorrect. Therefore the script is crashing

    // 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'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应