user2154508
user2154508

Reputation: 345

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

Upvotes: 0

Views: 665

Answers (2)

echo_Me
echo_Me

Reputation: 37233

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

Upvotes: 1

amof
amof

Reputation: 416

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;

Upvotes: 0

Related Questions