Johanne Smith
Johanne Smith

Reputation: 75

SELECT Count php/sql

I am trying to store a mysql value into a php variable. I have the following query which I know works. However, I the value for $count is always 0. Can someone explain what I need to do to get the count value? The count should be the count of x's w here name_x=.$id.

$query = "SELECT COUNT(name_x) FROM Status where name_x=.$id.";
$result = mysql_query($query);
$count = $result;

Upvotes: 0

Views: 16120

Answers (4)

CFL_Jeff
CFL_Jeff

Reputation: 2719

You are missing single quotes around $id. Should be name_x = '" . $id . "'";

Upvotes: 0

Riskhan
Riskhan

Reputation: 4470

please try it

$query = "SELECT COUNT(*) FROM Status where name_x=$id";
$result = mysql_query($query);
$count = mysql_result($result, 0); 

Upvotes: 0

user319198
user319198

Reputation:

Is first letter in table name is really capital. Please check it first.

or Try :

    $query = "SELECT COUNT(*) as totalno FROM Status where name_x=".$id;
    $result = mysql_query($query);
    while($data=mysql_fetch_array($result)){
    $count = $data['totalno'];
    }
echo $count;  

Upvotes: 1

Aaron W.
Aaron W.

Reputation: 9299

$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];

Upvotes: 1

Related Questions