查询计数和功能无法正常工作

时间:2011-05-16 00:17:18

标签: php mysql sql

我使用下面的代码选择项目并使用$ result变量进行计数。如果小于1,则表示添加更多,如果大于5,则表示查看全部。它适用于不到1但不会超过5.我做得对吗?

//查询

$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(mysql_num_rows($result) > 5) {
  echo "view all";
}

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

1 个答案:

答案 0 :(得分:2)

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

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

应该是

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

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

由于您使用前一个mysql_fetch_array()循环

耗尽了结果集