简体   繁体   中英

MySQL - Show result of count

I just trying to show a result of a consult in MySQL (PHP). The code is:

$example = mysql_query("SELECT count(*) as text FROM table WHERE name = '$name'");
$qtd = mysql_num_rows($example);
while($data = mysql_fetch_array($qtd)){
$count = $data["text"];
}

echo "<h3>($count)</h3>";

Error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\\AppServ\\www\\Site\\index.php on line 9

CHANGE THIS

 while($data = mysql_fetch_array($qtd)){
 $count = $data["text"];
 }

to

while($data = mysql_fetch_array($example)){
 $count = $data["text"];
  }

EDIT : you dont need to do a while loop here.

you should just do like that

     $example = mysql_query("SELECT count(*) as text FROM table WHERE name = '$name'");
     $data = mysql_fetch_array($example) ;
     $count = $data["text"];
     echo "<h3>".$count."</h3>";

This should work, you don't need to use a while statement.

$query = mysql_query("SELECT * FROM table WHERE name = '$name'");
$count = mysql_num_rows($query);
echo $count;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM