PHP MySQL我在get_results()处准备的语句中断 [英] PHP MySQLI prepared statement breaks at get_results()

查看:39
本文介绍了PHP MySQL我在get_results()处准备的语句中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mysqli准备的语句非常陌生,实际上这是我的第一次尝试.我有这段代码,并且在每个命令之间放置了回声,并且显示aaa和bbb但不显示ccc,我在这里做什么错了?

I am very new at mysqli prepared statements, in fact this is my first try at it. I have this block of code and I put echos inbetween each command, and it displays aaa and bbb but not ccc, what am i doing wrong here?

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

no errors come up, just a blank screen:

<?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();

?>

更新,我可以通过执行以下操作来实现此目的:

UPDATE I was able to get this working, by doing the following:

<?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行数据,如何获得所有数据行?

but this only gives me 1 row of data, how do I get all rows of data?

推荐答案

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

To use get_result() you must use the mysqlnd driver. This is enabled by default in PHP 5.4 and later. If you're using an earlier version of PHP, you have to do some installation to get mysqlnd to work. See http://php.net/manual/en/mysqlnd.install.php

如果使用get_result(),则无需绑定任何内容.您只需将每一行作为数组获取,然后将列作为该数组的元素引用:

If you use get_result(), then you don't need to bind anything. You just fetch each row as an array, and reference the columns as elements of that array:

    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 you don't use get_result(), you use Mysqli in the old manner, binding variables to columns, and calling fetch() to populate the variables. But you need to run a loop until fetch() returns NULL when the result is finished.

        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();
    }

这篇关于PHP MySQL我在get_results()处准备的语句中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆