加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:47:35 所属栏目:PHP教程 来源:网络整理
导读:我对 mysqli准备好的陈述很新,其实这是我第一次尝试它.我有这个代码块,我把echos放在每个命令之间,它显示aaa和bbb但不是ccc,我在这里做错了什么? 没有错误出现,只是一个空白屏幕: ?php $mysqli = new mysqli("localhost","username","password","database"
我对 mysqli准备好的陈述很新,其实这是我第一次尝试它.我有这个代码块,我把echos放在每个命令之间,它显示aaa和bbb但不是ccc,我在这里做错了什么?

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

<?php

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

        if (mysqli_connect_errno()) {
                printf("Connect failed: %sn",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 %sn",$title);
                }

                echo 'ddd';

                $stmt->close();

        }

        $mysqli->close();

?>

更新通过执行以下操作,我能够实现此功能:

<?php

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

            if (mysqli_connect_errno()) {
                    printf("Connect failed: %sn",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 %sn",$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 %sn",$title,$community);
            }
            $stmt->close();
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读