加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

计算OOP PHP中返回行的数量

发布时间:2020-05-25 08:48:50 所属栏目:PHP 来源:互联网
导读:我正在学习使用 MySQL的OOP PHP.我试图运行一个应该打印返回的行数的查询.我的php代码如下: $con= mysqli_connect(localhost,root,,dealinte_cms) or die(mysqli_error());$email=joy1@gmail.com;if($r=$con-prepare(SELECT * FROM use

我正在学习使用 MySQL的OOP PHP.我试图运行一个应该打印返回的行数的查询.我的php代码如下:

$con=  mysqli_connect('localhost','root','','dealinte_cms') or die(mysqli_error());
$email="joy1@gmail.com";
if($r=$con->prepare("SELECT * FROM user WHERE email=? ")){
$r->bind_param("s",$email);
    if(!$r->execute()){
        echo mysqli_errno($con);
    }
    else{
        $rowCount=$r->num_rows;
        echo $rowCount;
    }
}

在我的数据库中,电子邮件是4行,所以它应该打印4,但它显示0

我的代码有什么问题?

你必须 store result
$r->store_result();

在检查行数之前,也是如此.请参阅手册页上的此注释

Please be advised,for people who sometimes miss to read this important Manual entry for this function:

If you do not use mysqli_stmt_store_result( ),and immediatley call this function after executing a prepared statement,this function will usually return 0 as it has no way to know how many rows are in the result set as the result set is not saved in memory yet.

小费

由于您正在使用mysql学习OOP PHP,因此请务必注意,您为mysql创建的连接采用过程式语法.虽然在PHP中你可能会侥幸逃脱,但在许多其他语言中你不会.为什么不立即将其转换为OOP语法?

$con= new mysqli('localhost','my_user','my_password','my_db');

(编辑:安卓应用网)

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

    推荐文章
      热点阅读