Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有php的PreparedStatement select未按预期工作_Php_Sql_Select_Prepared Statement - Fatal编程技术网

带有php的PreparedStatement select未按预期工作

带有php的PreparedStatement select未按预期工作,php,sql,select,prepared-statement,Php,Sql,Select,Prepared Statement,我有一个疑问: SELECT id, result, ip_address, added_date FROM results WHERE course_id = ( SELECT id FROM courses WHERE course = 'informatica' AND macro_course_id = ( SELECT id FROM macro_courses WHERE macro_course = 'scienze-matematiche-fisiche-e-natura

我有一个疑问:

SELECT id, result, ip_address, added_date
FROM results
WHERE course_id = ( 
SELECT id
FROM courses
WHERE course =  'informatica'
AND macro_course_id = ( 
SELECT id
FROM macro_courses
WHERE macro_course =  'scienze-matematiche-fisiche-e-naturali'
AND organization_id = ( 
SELECT id
FROM organizations
WHERE organization =  'universita-degli-studi-di-torino'
AND city_id = ( 
SELECT id
FROM cities
WHERE city =  'torino'
AND region_id = ( 
SELECT id
FROM regions
WHERE region =  'piemonte' ))))) ORDER BY id DESC
我用这个代码来做一个准备好的声明

public function getResults($region, $city, $organization, $macro_course, $course) { //works
    //added_date=datetime : YYYY-MM-DD HH:mm:ss
    echo "SELECT id, result, ip_address, added_date
    FROM results
    WHERE course_id = ( 
    SELECT id
    FROM courses
    WHERE course =  '$course'
    AND macro_course_id = ( 
    SELECT id
    FROM macro_courses
    WHERE macro_course =  '$macro_course'
    AND organization_id = ( 
    SELECT id
    FROM organizations
    WHERE organization =  '$organization'
    AND city_id = ( 
    SELECT id
    FROM cities
    WHERE city =  '$city'
    AND region_id = ( 
    SELECT id
    FROM regions
    WHERE region =  '$region' ))))) ORDER BY id DESC"; //just for me to know what query is being executed 


    if ($stmt = $this->mysqli->prepare(("
    SELECT id, result, ip_address, added_date
    FROM results
    WHERE course_id = ( 
    SELECT id
    FROM courses
    WHERE course =  ?
    AND macro_course_id = ( 
    SELECT id
    FROM macro_courses
    WHERE macro_course =  ?
    AND organization_id = ( 
    SELECT id
    FROM organizations
    WHERE organization =  ?
    AND city_id = ( 
    SELECT id
    FROM cities
    WHERE city =  ?
    AND region_id = ( 
    SELECT id
    FROM regions
    WHERE region =  ? ))))) ORDER BY id DESC

  "))) {
        $return = array();
        $stmt->bind_param('sssss', $course, $macro_course, $organization, $city, $region);
        $stmt->execute();
        if ($stmt->fetch()) {
            $i = 0;
            while ($row = $stmt->fetch()) {
                print_r($row);//this is never reached
                continue;
                $s = new Result($row['result'], $row['added_date'], $row['id']);
                $return[$i] = $s;
                $i+=1;
            }
        }
    }
    return $return;
}
问题是该函数返回0行和0个错误(使用
$this->mysqli->error
进行检查),似乎$row=$stmt->fetch()总是false

但是,如果我复制并在PHPMyAdmin上执行我在函数顶部得到的输出,我会看到

Showing lines 0 - 0 ( 1 total, Query time 0.0003 sec)

因此,查询返回一行,但php无法捕获它。我错过了什么?如何修复此问题?

因为您在此处使用了两次
$stmt-fetch()

if ($stmt->fetch()) {
        $i = 0;
        while ($row = $stmt->fetch()) {
删除
如果($stmt->fetch())
条件,它将按预期工作

编辑

来自文档

请注意,在调用$stmt->fetch()之前,应用程序必须绑定所有列。

在像这样调用
$stmt->fetch()
之前,必须先绑定结果

/* bind result variables */
$stmt->bind_result($name, $code);

您可以通过手动学习正确的mysqli语法来修复它,或者更好的方法是使用PDO。您是否检查了
query
是否在没有服务器端的情况下工作?查询本身看起来让我眼花缭乱。你不能直接用连接进行一次查询吗?他已经在使用pdo了…我想说,如果需要的话,重组他的表并使用连接,一次查询6次是很糟糕的…@Ranjith阅读了所有问题:
但是,如果我复制并在PHPMyAdmin上执行我在函数顶部得到的输出,我会看到“显示0-0行”(总共1个,查询时间0.0003秒)“
当然可以,否则PMA会显示有一些错误。有时最好关闭一两个过于本地化的问题,而不是回答它。只是为了它。