0

I need to retrieve the number of rows in my qrec_id in my table tbl_link_qa, which has values in them.

mysql_query("SELECT COUNT(qrec_id) from tbl_link_qa")or die(mysql_error());

But this doesn't seem to give any output.

----updated:

$x=0;

mysql_query("SELECT COUNT * from tbl_link_qa WHERE qrec_id != $x");

2
  • mysql_query() alone will not give any output. Check out the manual page for a full working example: php.net/manual/en/function.mysql-query.php Commented Feb 10, 2011 at 15:22
  • you have to get that value exactly the same way you're getting any other data. why not to apply code you already used for other mysql operations? Commented Feb 10, 2011 at 15:39

3 Answers 3

2

This wouldn't give any output because all it's doing is sending the query to the database. It's not actually collecting the results.

You need to assign the result of mysql_query() to a variable.

<?php
if ($result = mysql_query ('select count(*) from wherever;'))
{
    $row = mysql_fetch_assoc ($result);
    var_dump ($row);
}
else
    die ('some error message');
?>
Sign up to request clarification or add additional context in comments.

5 Comments

It gave this off. array(1) { ["count(*)"]=> string(2) "87" } I need to get the number of affected rows in qrec_id field which contained values.
I need to get this code to be able to come off with a "1 of n questions available". The n is the number of rows that contained values in qrec_id that had yet to have any corresponding answers to them.
array(1) { ["count(*)"]=> string(2) "59" } it gave me this instead.
It's returning an array. You do know how to access array members, right? If not, it's all there in the PHP manual.
I got it :) mysql_fetch_assoc(). I just need to figure out how it's gonna be from "1 of 5" to "2 of 5" when I click next
1

use this query instead to get number of columns with not null values:

SELECT SUM(qrec_id IS NOT NULL) FROM tbl_link_qa

or

SELECT count(*) FROM tbl_link_qa WHERE qrec_id IS NOT NULL

and @Gordon script

2 Comments

or maybe ´SELECT SUM(qrec_id IS NULL) AS available, count(*) AS total FROM tbl_link_qa´ depends what it means if the field is NULL
Thank you! I got it now :) I just have to figure out my next step. Turning "1 of 5" to "2 of 5" when I click the next button and "1 of 5" back when I click my previous button.
1

You have a full example in the manual page:

http://es.php.net/mysql_query

See example #2.

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.