Jump to content
Larry Ullman's Book Forums

Recommended Posts

hi,

I am try to learn PHP and MySQL with the help of Larry's excellent book. Most of the time, I find the book easy to follow and I can find the answer I need, however I have now hit a wall!

I want to count the amount of rows in a table that meet a certain criteria and then use this number in a PHP while loop.

My query is - 

 

$SQL_count = SELECT COUNT(*) FROM college_notes WHERE link_code = 'CHES'

and I used this

$result_count = mysqli_query($dbc, $SQL_count);

 

My problem is that the variable $result_count doesnt seem to be usable. If I use echo to dump it on the screen, i get this -

 

Catchable fatal error: Object of class mysqli_result could not be converted to string in/home/www

I have also tried using AS to get an alias and $result_count['alais'] but got a message stating that the variable wasnt an array.

 

Thanks in advance for any help!

Dave

Link to comment
Share on other sites

You need to place your query into a String (quotes). It won't work without.

$query = "SELECT COUNT(*) AS numberOfRows FROM college_notes WHERE link_code = 'CHES'";

$result = mysqli_query($dbc, query); // Result resource

$row = mysqli_fetch_array('MYSQLI_ASSOC'); // Use something like this to get the result
echo $row['numberOfRows'];

Notice also that the $result variable will NOT hold the result from your query, but a result resource you can use to get it. It's as simple as adding one more step to your code.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...