Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > Using MySQL with PHP Wish List | Report Bug | About Me ]

9.4.2     Querying and formatting: mysql_query() and mysql_num_rows()

This is NOT the latest copy of this book; click here for the latest version.

resource mysql_query ( string query [, resource link_identifier])

int mysql_num_rows ( [resource result])

The majority of your interaction with MySQL in PHP will be done using the mysql_query() function, which takes one parameter - the SQL query you want to perform. It will then perform that query and return a special resource known as a MySQL result index - this resource contains all the rows that matched your query.

This result index resource is the return value of mysql_query(), and you should save it in a variable for later use - whenever you want to extract rows from the results, count the number of rows, etc, you need to use this value.

One other key function is mysql_num_rows(), which takes a MySQL result index as its parameter, and returns the number of rows inside that result - this is the number of rows that matched the query you sent in mysql_query(). With the two together we can write our first database enabled script:

<?php
    mysql_connect
("localhost", "phpuser", "alm65z");
    
mysql_select_db("phpdb");
    
$result = mysql_query("SELECT * FROM usertable");
    
$numrows = mysql_num_rows($result);
    print
"There are $numrows people in usertable\n";
?>

As you can see, we capture the return value of mysql_query() inside $result, then use on the very next line - this MySQL result index is used quite heavily, so it is important to keep track of it. The exception to this is when you are executing a write query in MySQL - you might not want to know the result.

One helpful feature of mysql_query() is that it will return false if the query is syntactically invalid - that is, if you have used a bad query. This means that very often it is helpful to check the return value even if you are writing data - if the data was not written successfully, mysql_query() will tell you so with the return value.





<< 9.4.1 Connecting to a MySQL database: mysql_connect(), mysql_select_db()   9.4.3 Disconnecting from a MySQL database: mysql_free_result() and mysql_close() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A L2M User - 06 Sep 2008

This is what I get when sending messages to someone..
Never happend before until the whole website crashed a time ago.
Now I get the following error everytime:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/look2me.nl/httpdocs/reactie_toevoegen.php on line 159

And I can't post anything because of this..
You guys know what the problem might be ?

Tnx

D.

charon - 06 Sep 2008

How do I check the SQL CODE returned from the query executed with help of mysql_query() funtion?

wood - 06 Sep 2008

thanks Mercutio i was testing this on my account and kept getting an error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

so i had no clue what was going on but once i added the or die i saw that i had my database name wrong and was able to fix it.

Mercutio - 06 Sep 2008

If you are going to provide snippets for people to copy and paste, at least use some form of error reporting. Thisis one of the most annoying features about books like these.

If you had written: mysql_query(..) or die(...); you would have helped many people immediately.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow