dongquelu1239 2018-08-26 01:33
浏览 59

为什么我的PDO $ stmt-> bind_result()函数调用在执行SELECT查询后挂起?

I have a MySQL database with table "Test" that has one column "TestData". There are three records with the following values for TestData: "This is value 1", "Here is another string", and "Third just for luck".

I wrote the following PHP code to retrieve the records.

<?php

try {
    $hostname = "redacted";
    $username = "redacted";
    $password = "redacted";
    $database = "redacted";

    $conn = new PDO("mysql: host=$hostname; dbname=$database", $username, $password);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT TestData FROM Test";

    $stmt = $conn->prepare($sql);

    $stmt->execute();
}
catch(PDOException $e)
{
    $finalResult = $finalResult . "," . $e->getMessage();
}

echo "you are here (" . $stmt->rowCount() . ")<br>";

if ($stmt->rowCount() > 0) {

    echo "found (" . $stmt->rowCount() . ")<br>";

    $stmt->bind_result($td);

    echo "bind successful<br>";

    while ($stmt->fetch()) {
        echo "testdata (" . $td . ")<br>";
    }
} else {
    echo "nothing found<br>";
}

?>

The result I receive is

you are here (3)

found (3)

The PHP script never gets to the "echo 'bind successful
'" statement. The "$stmt->bind_result($td);" statement hangs.

The query appears to work, given that rowCount = 3. I've used essentially the same structure to perform INSERTS that work properly.

What's wrong with what I'm doing? Thanks.

  • 写回答

2条回答 默认 最新

  • douji0108 2018-08-26 02:41
    关注

    I changed my code to the following and it works.

    <?php
    
        $hostname = "redacted";
        $username = "redacted";
        $password = "redacted";
        $database = "redacted";
    
        $conn = new mysqli($hostname, $username, $password, $database);
        if ($conn->connect_error) {
            fwrite(STDERR, "Connection failed: " . $conn->connect_error . "
    ");
            exit(1);
        }
    
        $sql = "SELECT TestData FROM Test WHERE ?";
    
        $stmt = $conn->stmt_init();
    
        if(!$stmt->prepare($sql)) {
            print "Failed to prepare statement
    ";
        } else {
            $stmt->bind_param("s", $condition);
        }
    
        $condition = "1 = 1";
    
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_array(MYSQLI_NUM)) {
            foreach ($row as $r) {    
                echo "testdata(" . $r . ")<br>";
            }
        }
    ?>
    

    No more mixing PDO and MySQLi for me. Thanks for the help. Sorry for the inconvenience.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度