mysqli num_rows嵌套查询

时间:2015-01-27 20:01:27

标签: php mysqli nested

我正在使用OOP将站点从MySQL更新到MySQLi。

在大多数情况下情况进展顺利,但这条线给了我神圣的麻烦:

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

我试过

$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;

但无济于事。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

OOP mysqli非常简单:)试试这个例子

$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

mysql_num_row()的mysqli是$query->num_rows()

我添加了一点额外的东西让你开始使用OOP mysqli :) 请注意,虽然我没有使用$get->num_rows(),但使用COUNT(*)代替更简单的示例。以下是您可能更熟悉的另一种方法:

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

另见here for more examples.

答案 1 :(得分:0)

感谢大家的帮助。事实证明,db连接被其中一个包含的文件过早关闭。我注意到,在这个项目中,我将代码从程序MySql转换为OOP MySQLi,数据库关闭之前没有影响网站的功能,但是一旦OOP MySqli进入,一定会这样做地点。我很好奇为什么。