Php 使此代码对PDO友好
我刚刚开始使用PDO而不是mysql函数。但现在我被困在php博客的一部分 如何使此代码对PDO更加友好:Php 使此代码对PDO友好,php,mysql,pdo,Php,Mysql,Pdo,我刚刚开始使用PDO而不是mysql函数。但现在我被困在php博客的一部分 如何使此代码对PDO更加友好: $total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num FROM php_blog")); $total_pages = ceil($total_results['num'] / $blog_postnumber); for($i = 1; $i <= $total_pages; $i
$total_results = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num
FROM php_blog"));
$total_pages = ceil($total_results['num'] / $blog_postnumber);
for($i = 1; $i <= $total_pages; $i++) {
if ($page == $i) {
echo "<span class='current'>$i</span>";
}
else {
echo "<a href=\"index.php?page=$i\">$i</a>";
}
}
$total\u results=mysql\u fetch\u数组(mysql\u查询(“选择COUNT(*)作为num
来自php_博客);
$total_pages=ceil($total_results['num']/$blog_postnume);
对于($i=1;$irowCount在PDO中不适用于mySQL。相反,您只需运行count(*)查询
$stmt=$db->exec(“从php_博客中选择count(*)作为num”);
$results=$stmt->fetch();
$total_results=$results['num'];
$total_pages=ceil($total_results/$blog_postnumber);
对于($i=1;$i-yo不需要rowCount()
,您需要fetchColumn()
。您到底尝试了什么?为什么这个基于其他[错误]答案的答案被高估了?您好,该代码显示了博客导航,但我得到一个错误,“警告:C:\wamp\www\meranhem\blogg.php中的非法字符串偏移量'num',在第70行”第70行是这一行:“$total_results=$results['num'];”请改为“select count(*)num..”。另外,“'print_r($results,true);”的输出是什么?如果它指向网页,请使用“view source”
<?php
$sql = "SELECT count(*) FROM `table` WHERE foo = bar";
$result = $con->prepare($sql);
$result->execute();
$number_of_rows = $result->fetchColumn();
$stmt = $db->exec( "select count(*) as num from php_blog" );
$results = $stmt->fetch();
$total_results = $results[ 'num' ];
$total_pages = ceil( $total_results / $blog_postnumber );
for($i = 1; $i <= $total_pages; $i++) {
if ($page == $i) {
echo "<span class='current'>$i</span>";
}
else {
echo "<a href=\"index.php?page=$i\">$i</a>";
}
}