这是我的代码:
<?php
function result($conn) {
global $number;
$sql = 'SELECT movie_name FROM movie_info LIMIT 3 OFFSET 0';
foreach ($conn->query($sql) as $row) {
print " ". $row['movie_name'] . " <br> ";
}
}
result($conn);
?>
我真的很困惑,怎么算表中的行数和打印?
如果你有一个PDO连接到你的数据库中的PDO对象 - (Let's称之为例如$ CON)你可以准备如下语句
$stmt = $con->prepare('SELECT movie_name FROM movie_info LIMIT 3 OFFSET 0');
// Then fire it up
$stmt->execute();
// Pick up the result as an array
$result = $stmt->fetchAll();
// Now you run through this array in many ways, for example
for($x=0, $n=count($result); $x < $n; $x++){
echo $result[$x]['movie_name'];
}
计数($结果)让您的结果号码
我想你可以做到这一点
SELECT count(*) FROM movie_info
上面的代码将返回存储在该表中的数据的计数,
这里是我的建议
$moviescount = $db->query('SELECT COUNT(`movie_name`) FROM `movie_info` LIMIT 3 OFFSET 0')->fetchColumn();
和
echo moviescount; // for example 10034