PHP MySQLI编写了get_results()的语句中断

我对 mysqli准备好的陈述很新,其实这是我第一次尝试它.我有这个代码块,我把echos放在每个命令之间,它显示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","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();

        }

?>

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

解决方法

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

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

if ($stmt = $MysqLi->prepare("SELECT title,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,image FROM `googleMaps`")) {
            $stmt->execute();
            $stmt->bind_result($title,$image);
            while ($stmt->fetch()) {
                    printf("%s %s\n",$title,$community);
            }
            $stmt->close();
    }

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...