我编写了一个函数来查找最接近提交时间戳的表中的时间戳。查询在Mysql中自行运行正常(通过phpmyadmin测试)。但是,当我通过php脚本执行它时,结果总是为NULL。
脚本和其他功能中的所有其他查询都正常工作。也没有错误。我必须遗漏一些但却看不到的东西。感谢您的任何帮助。
功能:
function GetClosestMatch($timestamps){
global $mysqli;
$closestmatch = array();
foreach($timestamps as $timestamp){
if($stmt1 = $mysqli->prepare("SELECT DISTINCT timestamp FROM winlog_data ORDER BY ABS(timestamp - ? ) LIMIT 1")){
mysqli_stmt_bind_param($stmt1,"i", $timestamp);
mysqli_stmt_execute($stmt1);
mysqli_stmt_store_result($stmt1);
mysqli_stmt_bind_result($stmt1, $db_timestamp);
$closestmatch[] = $db_timestamp;
}
}
return $closestmatch;
}
时间戳阵列:
array (size=12)
0 => int 1451602800
1 => int 1454281200
2 => int 1456786800
3 => int 1459461600
4 => int 1462053600
5 => int 1464732000
6 => int 1467324000
7 => int 1470002400
8 => int 1472680800
9 => int 1475272800
10 => int 1477954800
11 => int 1480546800
Closestmatch数组(来自函数的结果)
array (size=12)
0 => null
1 => null
2 => null
3 => null
4 => null
5 => null
6 => null
7 => null
8 => null
9 => null
10 => null
11 => null
在PHPmyadmin中查询结果:
SELECT DISTINCT timestamp FROM winlog_data ORDER BY ABS(timestamp - 1475272800 ) LIMIT 1
timestamp
1475271900
答案 0 :(得分:13)
您错过了对mysqli_stmt_fetch
的电话。
function GetClosestMatch($timestamps){
global $mysqli;
$closestmatch = array();
foreach($timestamps as $timestamp){
if($stmt1 = $mysqli->prepare("SELECT DISTINCT timestamp FROM winlog_data ORDER BY ABS(timestamp - ? ) LIMIT 1")){
mysqli_stmt_bind_param($stmt1,"i", $timestamp);
mysqli_stmt_execute($stmt1);
mysqli_stmt_store_result($stmt1);
mysqli_stmt_bind_result($stmt1, $db_timestamp);
while (mysqli_stmt_fetch($stmt1)) {
$closestmatch[] = $db_timestamp;
}
}
}
return $closestmatch;
}
此外,您正在混合和匹配过程和oop mysqli_ *调用。我建议你选择一种风格并坚持下去。 (虽然与你的问题没有关系)