Iain Simpson
Iain Simpson

Reputation: 8121

Mysql Count query returning "Query was empty"

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';
    } 
    ///////////////////////////////////////////////////////////////////////////////////////////////

Upvotes: 1

Views: 181

Answers (6)

chenio
chenio

Reputation: 592

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';
    } 

Upvotes: 1

v0d1ch
v0d1ch

Reputation: 2748

You have misstyped this

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

and also

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

Upvotes: 1

Johnson_145
Johnson_145

Reputation: 2032

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

$numberOfRows = mysql_num_rows($result8)

Upvotes: 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.

Upvotes: 2

HenchHacker
HenchHacker

Reputation: 1626

$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.

Upvotes: 2

eric.itzhak
eric.itzhak

Reputation: 16062

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.

Upvotes: 3

Related Questions