PHP 和 Num_Of_Rows() 函数的使用?
下面是我编写的一些 PHP 代码,当使用 num_of_rows() 时出现问题,它似乎不起作用,我不明白为什么?
<?php
try
{
$divMon_ID = array();
$divMon_Position = array();
$divMon_Width = array();
$divMon_Div = array();
$db = new PDO('sqlite:db/EVENTS.sqlite');
$result_mon = $db->query('SELECT * FROM Monday');
$totalRows = mysql_num_rows($result_mon);
//for($counter=1; $counter<=10; $counter+=1)
//{
//<div id="event_1" style="position:absolute; left: 0px; top:-39px; width:100px; font-family:Arial, Helvetica, sans-serif; font-size:small; border:2px blue solid; height:93px">
//$divMon_ID[]=$row['Id'];
//$divMon_Position[]=$row['Origin'];
//$divMon_P[]=$row['Position'];
//}
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
我知道它是“$totalRows = mysql_num_rows($result_mon);”声明,因为当我将其注释掉时,页面可以加载。
我是否以错误的方式使用该功能?
谢谢。
Below is some PHP code that i have written, the problem occurs when it gets to the use of the num_of_rows(), it just does not seem to work and i cant figure out why?
<?php
try
{
$divMon_ID = array();
$divMon_Position = array();
$divMon_Width = array();
$divMon_Div = array();
$db = new PDO('sqlite:db/EVENTS.sqlite');
$result_mon = $db->query('SELECT * FROM Monday');
$totalRows = mysql_num_rows($result_mon);
//for($counter=1; $counter<=10; $counter+=1)
//{
//<div id="event_1" style="position:absolute; left: 0px; top:-39px; width:100px; font-family:Arial, Helvetica, sans-serif; font-size:small; border:2px blue solid; height:93px">
//$divMon_ID[]=$row['Id'];
//$divMon_Position[]=$row['Origin'];
//$divMon_P[]=$row['Position'];
//}
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
I know that it is the "$totalRows = mysql_num_rows($result_mon);" statement because when i then comment it out, the page can load.
Am i using the function in the wrong way?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将 mysql_num_rows 与 pdo 一起使用。您必须使用 rowCount()
You cant use mysql_num_rows with pdo. You have to use rowCount()
如果您使用 mysql_connect 和其他 mysql_* 函数,则 mysql_num_rows 可以工作。当您使用 PDO 时,您必须使用 PDO 的方法,例如 行计数()
Mysql_num_rows would work if you used mysql_connect, and other mysql_* functions. As you are using PDO, you have to use PDO's methods, like rowCount()
您已经创建了一个连接到 SQLite 数据库的 PDO 对象。为什么 MySQL 函数应该知道如何处理这些问题?
query
方法返回一个PDOStatement
类型的对象,该对象具有rowCount
方法。使用它:也就是说,如果您在处理行之前甚至需要知道行总数。如果您需要知道的只是表中的行数,请考虑使用
COUNT
SQL 函数:SELECT COUNT(*) FROM Monday
。You've created a PDO object connecting to an SQLite database. Why should a MySQL function know how to deal with any of that?
The
query
method returns an object of typePDOStatement
, which has arowCount
method. Use that:That is, if you even need to know the total number of rows before you process them. If all you need to know is the number of rows in the table, consider using the
COUNT
SQL function instead:SELECT COUNT(*) FROM Monday
.