【问题标题】:Query count and function not working properly查询计数和功能无法正常工作
【发布时间】:2011-05-16 00:17:17
【问题描述】:

我正在使用下面的代码选择项目并使用 $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";
} ?>

【问题讨论】:

  • 标准示例的输出是什么?

标签: php mysql sql


【解决方案1】:
 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() 循环耗尽了结果集

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-05
    • 2017-09-23
    • 2022-01-05
    • 2021-09-13
    • 1970-01-01
    • 2013-11-06
    • 2013-10-29
    相关资源
    最近更新 更多