1

I am using the following script to try and count the rows in a table, the problem I keep getting is the error :

Query was empty Quiz Name

and a blank page. Im new to COUNT, so I think I might be making a mess of it :-S . My database layout is as follows :

itsnb_chronoforms_data_createquestions cf_id ,cf_uid,cf_created ,cf_modified, cf_ipaddress,    cf_user_id, quizID, questionID, quizquestion, quizanswer1, quizanswer2, quizanswer3, quizanswer4,     questionformat ,correctanswer

The script I am working on is :

    // Define Quiz Variables
    $quiz = $row['quizID'];
    $quizcfid = $row['cf_id'];
    $quizname = $row['quizname'];
    // Finish Define Quiz Variables  

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Make a MySQL Connection

    $query8 = "SELECT COUNT(*) as 'numberofquestions' FROM employees WHERE quizID='$quiz'"; 

    $result8 = mysql_query($query) or die(mysql_error());

    // Print out result
    while($row8 = mysql_fetch_array($result8)){
        echo 'There are '. $row8['COUNT(quizID)'] . ' questions';
    } 
    ///////////////////////////////////////////////////////////////////////////////////////////////
0

6 Answers 6

3

Your problem is that you are running the wrong query, you are running $query and not $query8

change to this :

$result8 = mysql_query($query8) or die(mysql_error());

in a second glance, you should also change to :

echo 'There are '. $row8['numberofquestions'] . ' questions';

as you set numberofquestion to be the count alias.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that has gotten rid of the error, but I now get There are questions echo'd out, it doesn't appear to be echoing the number of questions.
Thanks that's sorted now, I thought I had copied and pasted that correctly but apparently not........Its been a long day ! :-)
You welcome. i've browsed through your questions and you should start accpeting answers on your questions.
2
$result8 = mysql_query($query) or die(mysql_error());

should be

$result8 = mysql_query($query8) or die(mysql_error());

since i'm guessing the variable $query is empty which you were passing it before.

As for COUNT() stay away from COUNT(*), instead use COUNT(field_name) and for even faster results ensure that the field_name is in the index being used.

Count simply counts the total rows returned for the specified field.

Also, be carefull of using COUNT(*) in innodb as it will force a TABLE SCAN if not used with a WHERE clause on an index.

Comments

2

Replace your following line:

echo 'There are '. $row8['COUNT(quizID)'] . ' questions';

for this one:

echo 'There are '. $row8['numberofquestions'] . ' questions';

As numberofquestions is the name of the only field from your query result set.

Comments

2

To get the number of rows in your result use mysql_num_rows:

$numberOfRows = mysql_num_rows($result8)

Comments

1

You have misstyped this

$result8 = mysql_query($query8) or die(mysql_error());

and also

echo 'There are '. $row8['numberofquestions'] . ' questions';

Comments

1

The sentence sql i see rigth. But i use this

$result8 = mysql_query($query) or die(mysql_error());
$count = mysql_fetch_row($result8);


echo 'There are '.$count[0].' '. questions';

Other Option:

 while($row8 = mysql_fetch_array($result8,MYSQL_ASSOC)){
        echo 'There are '. $row8['numberofquestions'] . ' questions';
    } 

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.