我的PHP MySQL while循环不返回结果

时间:2011-11-13 05:21:50

标签: php mysql loops while-loop

<?php
$sqls = mysql_query("SELECT weight FROM $usertablestats")
or die ("Query failed: " . mysql_error() . " Actual query: " . $query);
$ct = mysql_query ("COUNT * '$sqls'");

if ($ct > 0) {
   while ($row = mysql_fetch_array($sqls));{
      $weight = $row["weight"];
      echo "C" . $weight;
    }}
else {
  echo "No stats found";
}
?>

即使我在表格中有数据,也会输出“找不到统计数据”。

<?php
$sqls = mysql_query("SELECT weight FROM $usertablestats")
or die ("Query failed: " . mysql_error() . " Actual query: " . $query);
$ct = mysql_num_rows($sqls);

if ($ct > 0) {
   while ($row = mysql_fetch_array($sqls));{
      $weight = $row["weight"];
      echo "C" . $weight;
    }}
else {
  echo "No stats found";
}
?>

这不会返回任何内容。根本没有回音。

我已经通过以下方式检查是否正在访问:

<?php
   $sqls = mysql_query("SELECT weight FROM $usertablestats")
   or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

   $row = mysql_fetch_array($sqls);

   echo $row;
?>

它确实返回了第一个条目。

4 个答案:

答案 0 :(得分:1)

你有分号:

while ($row = mysql_fetch_array($sqls));{
//should be
while ($row = mysql_fetch_array($sqls)){

是否导致问题

答案 1 :(得分:0)

试试这个:

<?php
$sqls = mysql_query("SELECT weight FROM $usertablestats")
or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

int $ct = 0;
while ($row = mysql_fetch_array($sqls)){
    $weight = $row["weight"];
    echo "C" . $weight;
    $ct++;
}

if ($ct == 0) {
    echo "No stats found";
}

?>

如果不起作用,请确保$usertablestats具有正确的值。

答案 2 :(得分:0)

试试这个。

while ($row = mysql_fetch_array($sqls,MYSQL_ASSOC)){
    $weight = $row["weight"];
    echo "C" . $weight;
    $ct++;
}

答案 3 :(得分:0)

<?php
$sqls = mysql_query("SELECT * FROM $usertablestats")
or die ("Query failed: " . mysql_error() . " Actual query: " . $query);

if (mysql_num_rows($sqls)!=0) {
   while ($row = mysql_fetch_assoc($sqls)){
   $weight = $row["weight"];
   echo "C" . $weight;
}}
else {
   echo "No stats found";
}
?>