0

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;
4
  • You want to retrieve the results using mysql_result on $result. See: php.net/manual/en/function.mysql-result.php Commented Mar 3, 2012 at 5:10
  • the problem is in COUNT(x).. just use COUNT(*). Also I believe id is INT... Commented Mar 3, 2012 at 5:13
  • @rjz: when I try to set $count = mysql_result($result,0), I don't get back any values for count. Commented Mar 3, 2012 at 5:26
  • possible duplicate of Output value from array Commented Mar 3, 2012 at 6:13

4 Answers 4

1

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;  
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, the first label in the table is really capital. I tried your suggestion to no avail and I even tried the following and it gave the error EXPECTS PARAMETER 1 TO BE RESOURCE, BOOLEAN GIVEN:$query = "SELECT name_x FROM Status where name_x=.$id."; $result = mysql_query($query); $count = mysql_num_rows($result);
EXPECTS PARAMETER 1 TO BE RESOURCE, BOOLEAN GIVEN: I am sure query is not ok. check fields & table name and make sure u are connected to correct database.
Thank you Teez, I saw the error, I had the .$id in quotes. Sorry for all of the confusion!
1
$query = "SELECT COUNT(*) FROM `Status` where `name_x`= $id";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$count = $row[0];

Comments

0

please try it

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

1 Comment

When I try the Select command in mysql, I get back a result; however, in the php file, I don't get anything back. I have used the * and that doesn't solve the problem either.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.