当前位置:  开发笔记 > 编程语言 > 正文

Mysql查询不能在PHP中工作,在mysql中工作正常

如何解决《Mysql查询不能在PHP中工作,在mysql中工作正常》经验,为你挑选了1个好方法。

我写了一个函数来查找最接近提交的时间戳的表中的时间戳.查询在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 

yivi.. 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_*调用.我建议你选择一种风格并坚持下去.(虽然与你的问题没有关系)



1> yivi..:

你错过了你的电话 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_*调用.我建议你选择一种风格并坚持下去.(虽然与你的问题没有关系)

推荐阅读
小白也坚强_177
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有