dtnpf35197 2015-01-27 20:01
浏览 9

mysqli num_rows嵌套查询

I'm updating a site from MySQL to MySQLi, using OOP.

For the most part things are going well, but this line is giving me divine trouble:

$pager_total = mysql_num_rows(mysql_query($MySQLctr,$MyDB));

I've tried

$pager_total = $MyDB->query($MySQLctr)->num_rows;
and
$pager_total = ($MyDB->query($MySQLctr))->num_rows;
and
$new_object = $MyDB->query($MySQLctr);
$pager_total = $new_object->num_rows;

but no avail.

Any thoughts?

  • 写回答

2条回答 默认 最新

  • doudie2693 2015-01-27 20:22
    关注

    OOP mysqli is very simple :) try this example

    $DBH=new mysql('location of database','username','password','database');
    $get=$DBH->prepare('SELECT COUNT(*) FROM table');//notice the count(*) in the query, it's not the same as mysql_num_row() but works in the same way
    //prepare is also a more secure way of processing query, please look up the difference between prepare and query though.
    //$DBH->prepare('SELECT * FROM table WHERE ID=2'); will not work.
    $get->execute();//execute the above prepared statement
    $get->bind_result($count);//get the result of the query, should be whatever COUNT(*) returns
    $get->close();//don't forget to close your connection
    

    The mysqli of mysql_num_row() is $query->num_rows().

    I added a little bit extra to get you started with OOP mysqli :) Notice though that I didn't use $get->num_rows() but COUNT(*) instead for a more simple example. Here is another method you may be more familiar with:

    $counter=$DBH->query('SELECT * FROM table');
    echo$counter->num_rows();
    

    Also see here for more examples.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大