2011-11-13 76 views
0
<?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"; 
} 
?> 

即使表中有數據,也會輸出「No stats found」。我的PHP MySQL While while循環不返回結果

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

而且它返回的第一個條目。

回答

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

1

你分號,而:

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

是不是造成問題

+0

//謝謝!我是新來的,我知道這一定是我的一些愚蠢的東西。它效果很好。我不認爲有辦法從第一行中減去最後一行? –

0

試試這個。

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

 

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