我必须在像这样的for循环中运行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查询......
如何等待php for mysql完成查询?
编辑:
好的,我在这里创建循环时出现了错误,但这是一种分心!这不是复制/粘贴
在此之后我写了
ID result was always related to first $date_start, then first sql query...
这意味着这不是代码问题,循环工作,否则我有一个php错误!
我将尝试使用MYSQL错误报告
答案 0 :(得分: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'));
}