开发者

Query count and function not working properly

I am using the code below to select items and using the $result variable to do a count. if there are less than 1 it will say add more and if there are more than 5 it will say view all. It works for less than 1 but it won't for more than 5. Am i doing it right?

//Query

$sql = "SELECT id, name, why, date_time 
          FROM tabs 
         WHERE p_id = '$pid'
      ORDER BY id 
         LIMIT 0, 5";

$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());

if ($result == "") {
  echo "";
}

echo "";

$rows = mysql_num_rows($result);

if($rows == 0) {
  print("");
} elseif($rows > 0) {
  while($row = mysql_fetch_array($query)) {
    $name = $row['name'];
    $w = nl2br($row['why']);
    $y = $row['date_time'];

    print("echoing contents here");
  }
}

if(mys开发者_如何学Goql_num_rows($result) > 5) {
  echo "view all";
}

if(mysql_num_rows($result) < 1) {
  echo "add one";
} ?>


 if(mysql_num_rows($result) > 5) {
  echo "view all";
  }

 if(mysql_num_rows($result) < 1) {
   echo "add one";
  } 

Should be

 if($rows > 5) {
  echo "view all";
  }

 if($rows < 1) {
   echo "add one";
  }

Since you exhausted the result set with the previous mysql_fetch_array() loop

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜