【问题标题】:PHP MySQLI prepared statement breaks at get_results()PHP MySQLI 准备语句在 get_results() 处中断
【发布时间】:2014-09-17 17:36:01
【问题描述】:

我对 mysqli 准备语句非常陌生,事实上这是我第一次尝试它。我有这段代码,我在每个命令之间添加了回显,它显示 aaa 和 bbb 但不显示 ccc,我在这里做错了什么?

没有错误出现,只是一个空白屏幕:

<?php

        $mysqli = new mysqli("localhost", "username", "password", "database");

        if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
        }

        if ($stmt = $mysqli->prepare("SELECT title FROM `in-the-press`")) {
                $stmt->execute();

                echo 'aaa';

                $stmt->bind_result($title);

                echo 'bbb';

                $result = $stmt->get_result();

                echo 'ccc';

                while ($stmt->fetch()) {
                        printf("%s %s\n", $title);
                }

                echo 'ddd';

                $stmt->close();

        }

        $mysqli->close();

?>

更新通过执行以下操作,我能够使其正常工作:

<?php

            $mysqli = new mysqli("localhost", "username", "password", "database");

            if (mysqli_connect_errno()) {
                    printf("Connect failed: %s\n", mysqli_connect_error());
                    exit();
            }

            if ($stmt = $mysqli->prepare("SELECT id, community, map, image FROM `googleMaps`")) {

                $stmt->execute();

                $stmt->bind_result($id, $community, $map, $image);

                $stmt->fetch();

                printf($id . ' ' . $community . ' ' . $map . ' ' . $image);

                $stmt->close();

        }

?>

但这只给了我 1 行数据,我如何获取所有行数据?

【问题讨论】:

  • 当没有变量绑定到查询时,不需要准备好的语句——只需使用query()。结果集对象(来自query() 的返回值或来自准备好的语句get_result() 的返回值)可以通过foreach() 进行迭代。 stackoverflow.com/a/52323556/2943403

标签: php mysqli


【解决方案1】:

要使用get_result(),您必须使用 mysqlnd 驱动程序。这在 PHP 5.4 及更高版本中默认启用。如果您使用的是早期版本的 PHP,则必须进行一些安装才能使 mysqlnd 工作。见http://php.net/manual/en/mysqlnd.install.php

如果你使用get_result(),那么你不需要绑定任何东西。您只需将每一行作为一个数组获取,并将列作为该数组的元素引用:

    if ($stmt = $mysqli->prepare("SELECT title, community, map, image  FROM `googleMaps `")) {
            $stmt->execute();
            $result = $stmt->get_result();
            while ($row = $result->fetch_assoc()) {
                    printf("%s %s\n", $row["title"], $row["community"]);
            }
            $stmt->close();
    }

如果不使用get_result(),则以旧方式使用Mysqli,将变量绑定到列,并调用fetch() 来填充变量。但是你需要运行一个循环,直到结果完成时fetch()返回NULL。

        if ($stmt = $mysqli->prepare("SELECT title, community, map, image FROM `googleMaps`")) {
            $stmt->execute();
            $stmt->bind_result($title, $community, $map, $image);
            while ($stmt->fetch()) {
                    printf("%s %s\n", $title, $community);
            }
            $stmt->close();
    }

【讨论】:

    【解决方案2】:

    您需要循环打印结果,例如,您需要为找到的每个结果回显

    http://php.net/manual/en/control-structures.for.php

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多