简体   繁体   English

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

[英]Why does my PDO $stmt->bind_result() function call hang after executing a SELECT query?

I have a MySQL database with table "Test" that has one column "TestData". 我有一个带有表“ Test”的MySQL数据库,该表具有一列“ TestData”。 There are three records with the following values for TestData: "This is value 1", "Here is another string", and "Third just for luck". 有三个记录,其中TestData具有以下值:“ This is value 1”,“ Here is another string”和“ Third just for lucky”。

I wrote the following PHP code to retrieve the records. 我编写了以下PHP代码来检索记录。

<?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) 你在这里(3)

found (3) 找到(3)

The PHP script never gets to the "echo 'bind successful PHP脚本永远无法成功完成“回显”绑定
'" statement. The "$stmt->bind_result($td);" statement hangs. '“语句。” $ stmt-> bind_result($ td);“语句挂起。

The query appears to work, given that rowCount = 3. I've used essentially the same structure to perform INSERTS that work properly. 给定rowCount = 3,该查询似乎可以正常工作。我使用了基本上相同的结构来执行可以正常工作的INSERTS。

What's wrong with what I'm doing? 我在做什么错了? Thanks. 谢谢。

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 . "\n");
        exit(1);
    }

    $sql = "SELECT TestData FROM Test WHERE ?";

    $stmt = $conn->stmt_init();

    if(!$stmt->prepare($sql)) {
        print "Failed to prepare statement\n";
    } 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. 我不再需要混合PDO和MySQLi。 Thanks for the help. 谢谢您的帮助。 Sorry for the inconvenience. 抱歉给你带来不便。

If you are just trying to get the items from the database using php pdo you need to store the results. 如果您只是想使用php pdo从数据库中获取项目,则需要存储结果。

$results = $stmt->fetch(); //will get one row

$results = $stmt->fetchAll(); //will take all results and store in an array

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM