James AraSmith
James AraSmith

Reputation: 1

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

This outputs "No stats found" even though I have data in the table.

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

This returns nothing. No echo at all.

I have checked to see if it is accessing by just using:

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

   $row = mysql_fetch_array($sqls);

   echo $row;
?>

And it does return the first entry.

Upvotes: 0

Views: 716

Answers (4)

vickyqiu
vickyqiu

Reputation: 9269

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

Upvotes: 0

no_freedom
no_freedom

Reputation: 1961

Try this.

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

 

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

You have semicolon in while :

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

Is that causing problem

Upvotes: 1

dimme
dimme

Reputation: 4424

Try this:

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

?>

If it doesn't work then make sure that $usertablestats has the right value.

Upvotes: 0

Related Questions