MySQLi预处理语句 - 不同步的命令仍返回结果

时间:2014-12-26 12:52:01

标签: php mysqli

我要么失去了情节,要么有人在玩我的想法:/

我已将此代码与我的应用程序的其余部分分开以尝试调试,也为了便于阅读而硬编码$ guideid。

我有以下代码(此脚本中没有其他内容作为测试,即没有其他查询)

     <?php
     define("DB_HOST", "127.0.0.1");
     define("DB_NAME", "xxxxx");
     define("DB_USER", "xxxxx");
     define("DB_PASS", "xxxxx");

     $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

     $stmt = $mysqli->prepare("SELECT `guides_listings`.`listing_id`, `guide_slug`, `guide_name_en`, listing_name, `listing_slug`, `slogo`.`filename` AS `slogoname`, `hlogo`.`filename` AS `hlogoname`, `vlogo`.`filename` AS `vlogoname`

                                      FROM `guides_listings`
                                      JOIN `guides` ON `guides_listings`.`listing_guide` = `guides`.`guide_id`
                                      LEFT JOIN `guides_listings_pics` AS `slogo`
                                        ON `slogo`.`listing_id` = `guides_listings`.`listing_id`
                                        AND `slogo`.`type` = 'slogo'
                                      LEFT JOIN `guides_listings_pics` AS `hlogo`
                                        ON `hlogo`.`listing_id` = `guides_listings`.`listing_id`
                                        AND `hlogo`.`type` = 'hlogo'
                                      LEFT JOIN `guides_listings_pics` AS `vlogo`
                                        ON `vlogo`.`listing_id` = `guides_listings`.`listing_id`
                                        AND `vlogo`.`type` = 'vlogo'
                                      WHERE (`slogo`.`filename` IS NOT NULL OR `hlogo`.`filename` IS NOT NULL OR `vlogo`.`filename` IS NOT NULL)
                                        AND `guides_listings`.`listing_guide` = ?
                                      GROUP BY `guides_listings`.`listing_id`
                                      ORDER BY RAND() 
                                      LIMIT 12");

    $stmt->bind_param("i",$guideid);
    $guideid = 2;   

    $stmt->execute();       

    $stmt->bind_result($listing_id,$guide_slug,$guide_name,$listing_name,$listing_slug,$slogo,$hlogo,$vlogo);           

    while($stmt->fetch()) {
        $results->data[] = array('listing_id'=>$listing_id,'guide_slug'=>$guide_slug,'guide_name'=>$guide_name,'listing_name'=>$listing_name,'listing_slug'=>$listing_slug,'slogo'=>$slogo,'hlogo'=>$hlogo,'vlogo'=>$vlogo);
    }

    $stmt->close();
    ?>

执行()

后会产生Commands out of sync; you can't run this command now

但是仍然会返回正确的结果。

我是否遗漏了一些明显的原因,为什么它仍然可以发出此错误?

修改

我刚刚进行了一些测试,这似乎发生在所有准备好的查询中,请参阅下面的示例:

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$stmt = $mysqli->stmt_init();
$stmt->prepare("SELECT `listing_id` FROM `guides_listings` LIMIT 1");
$stmt->execute(); 

print_r($mysqli);  

$stmt->bind_result($listing_id);           

$stmt->fetch();
$results->data[] = array('listing_id'=>$listing_id);
$stmt->close();

print_r导致:

mysqli Object
(
[affected_rows] => -1
[client_info] => 5.6.21
[client_version] => 50621
[connect_errno] => 0
[connect_error] => 
[errno] => 0
[error] => 
[error_list] => Array
    (
    )

[field_count] => 1
[host_info] => 127.0.0.1 via TCP/IP
[info] => 
[insert_id] => 0
[server_info] => 5.6.21
[server_version] => 50621
[stat] => Commands out of sync; you can't run this command now
[sqlstate] => HY000
[protocol_version] => 10
[thread_id] => 19462371
[warning_count] => 0
)

也许我真的在看这里的错误?

2 个答案:

答案 0 :(得分:1)

  • 在绑定之前分配$guideid

  • 尝试在$stmt->store_result()execute()之间致电bind_result()

  • 看起来$stmt实际上并未定义。通常我会$stmt = mysqli->prepare(...)

答案 1 :(得分:1)

没有准备好的声明:

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($stmt= $mysqli->query("SELECT `listing_id` FROM `guides_listings` LIMIT 1")) {
    $stmt->bind_result($listing_id);           
    $stmt->fetch();
    echo "listing_id = ". $listing_id;
    $stmt->close();
}else{
    printf("Error: %s\n", $mysqli->error);
}
$mysqli->close();

使用Prepared语句:

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$query = "SELECT `listing_id` FROM `guides_listings` LIMIT 1";
if ($stmt = $mysqli->prepare($query)) {
    $stmt->execute();
     /* store result */
    $stmt->store_result();
    if($stmt->num_rows > 0){
    $stmt->bind_result($listing_id);
        if ($stmt->fetch()) {
            echo "listing_id = ". $listing_id;
        }
    }
    /* free result */
    $stmt->free_result();
    $stmt->close();
}else{
    /*failed to prepare*/
    printf("Error: %s\n", $mysqli->error);
}
$mysqli->close();

准备好的声明(完整代码):

<?php
define("DB_HOST", "127.0.0.1");
define("DB_NAME", "xxxxx");
define("DB_USER", "xxxxx");
define("DB_PASS", "xxxxx");

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "
SELECT `guides_listings`.`listing_id`, 
       `guide_slug`, 
       `guide_name_en`, 
       listing_name, 
       `listing_slug`, 
       `slogo`.`filename` AS `slogoname`, 
       `hlogo`.`filename` AS `hlogoname`, 
       `vlogo`.`filename` AS `vlogoname` 
FROM   `guides_listings` 
       JOIN `guides` 
         ON `guides_listings`.`listing_guide` = `guides`.`guide_id` 
       LEFT JOIN `guides_listings_pics` AS `slogo` 
              ON `slogo`.`listing_id` = `guides_listings`.`listing_id` 
                 AND `slogo`.`type` = 'slogo' 
       LEFT JOIN `guides_listings_pics` AS `hlogo` 
              ON `hlogo`.`listing_id` = `guides_listings`.`listing_id` 
                 AND `hlogo`.`type` = 'hlogo' 
       LEFT JOIN `guides_listings_pics` AS `vlogo` 
              ON `vlogo`.`listing_id` = `guides_listings`.`listing_id` 
                 AND `vlogo`.`type` = 'vlogo' 
WHERE  ( `slogo`.`filename` IS NOT NULL 
          OR `hlogo`.`filename` IS NOT NULL 
          OR `vlogo`.`filename` IS NOT NULL ) 
       AND `guides_listings`.`listing_guide` = ? 
GROUP  BY `guides_listings`.`listing_id` 
ORDER  BY Rand() 
LIMIT  12 
";

$guideid = 2;  


if ($stmt = $mysqli->prepare($query)) { 
    $stmt->bind_param("i", $guideid);
    $stmt->execute();   
    $stmt->store_result();
    $stmt->bind_result($listing_id,$guide_slug,$guide_name,$listing_name,
                       $listing_slug,$slogo,$hlogo,$vlogo);           
    $results= array();     
    while($stmt->fetch()) {
        $results[] = array(
           'listing_id'=>$listing_id,'guide_slug'=>$guide_slug,
           'guide_name'=>$guide_name,'listing_name'=>$listing_name,
           'listing_slug'=>$listing_slug,'slogo'=>$slogo,
           'hlogo'=>$hlogo,'vlogo'=>$vlogo
        );
    }
    $stmt->free_result();
    $stmt->close();

}else{
    $results[] = array("Error:" => $stmt->error);
}

/* close connection */
$mysqli->close();
/* Output results in JSON*/
echo json_encode($results);