我收到警告,mysql_fetch_array()
期望参数1为资源。这是我获取帖子数量然后打印行数的代码。
<?php
$result = mysql_query("SELECT * FROM amityusers WHERE added_by='$user'");
while($array = mysql_fetch_array($result))
{
$hoot = $array['post'];
$num_hoot = mysql_num_rows($hoot);
}
?>
如何打印行数以及问题是什么?
答案 0 :(得分:4)
只需取出循环外的mysql_num_rows()
,然后将变量更改为$result
$num_hoot = mysql_num_rows($result);
答案 1 :(得分:2)
试试这个适合你的
$result = mysql_query("SELECT * FROM amityusers WHERE added_by='$user'") or die(mysql_error());
$num_row = mysql_num_rows($result);
$post = array();
if($num_row > 1)
{
while($array = mysql_fetch_array($result))
{
$post[]= $array['post'];
}
var_dump($post);
}
else
{
echo 'no row found';
}
答案 2 :(得分:1)
让我们将您的MySQL转换为MySQLi。把你的mysqli_num_rows放在循环之外。 AND 将mysqli_num_rows()
中的变量替换为 $ result 而不是 $ hoot 。不要忘记 echo :
<?php
$yourConnection=mysqli_connect("host","username","password","yourDatabase");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
$result = mysqli_query($yourConnection,"SELECT * FROM amityusers WHERE added_by='$user'");
while($array = mysqli_fetch_array($result)){
$hoot = $array['post'];
}
$num_hoot = mysqli_num_rows($result); /* PUT THIS OUTSIDE YOUR LOOP AND PUT $result INSTEAD OF $hoot VARIABLE*/
echo "Total result of ".$num_hoot;
?>
答案 3 :(得分:1)
试试这个,您可以使用count()
获取帖子
while($array = mysqli_fetch_array($result)){
$hoot = $array['post'];
echo $num_hoot = count($hoot);
}
答案 4 :(得分:0)
在$result
mysql_num_rows
$num_hoot = mysql_num_rows($result);
答案 5 :(得分:0)
首先,不要使用已弃用的mysql函数
$result = mysql_query("SELECT * FROM amityusers WHERE added_by='$user'") or die(mysql_error());
$num_hoot = mysql_num_rows($result);
while($array = mysql_fetch_array($result))
{
$post= $array['post'];
}
答案 6 :(得分:0)
尝试这种方式
$con = mysql_connect("localhost", "root", "");
$selectdb = mysql_select_db("mydbname",$con);
$result = mysql_query("select * from student");
$number_of_rows = mysql_num_rows($result);
echo "Number of rows fetched are : ". $number_of_rows;