簡體   English   中英

我的PHP MySQL while循環未返回結果

[英]My PHP MySQL while loop does not return results

<?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;
?>

並且它確實返回第一個條目。

您在以下時間內有分號:

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

是造成問題的嗎

<?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";
}
?>

嘗試這個:

<?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具有正確的值。

嘗試這個。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM