mysql_num_rows()
expects parameter 1 to be resource, but you gave it a string. You should make a query and obtain the resource returned by mysql_query()
.
For example:
$resource = mysql_query($login);
$rows = mysql_num_rows($resource);
To use the mysql_*
functions, you would do something like this:
mysql_connect('host', 'user', 'password'); // connect to server
mysql_select_db('database name'); // select the database you'll be working with
$result = mysql_query('SELECT test FROM test_table'); // make a query
while ($row = mysql_fetch_assoc($result)) { // go through the obtained results
echo $row['test']; // use the obtained results
}
mysql_close(); // close the database connection
If you want to use sprintf()
to compose the query, I suggest you create the string and store it in an appropriately named variable, to make things easier to follow:
$query = sprintf("SELECT test FROM test_table WHERE a = '%s' AND b = '%s'",
mysql_real_escape_string($a),
mysql_real_escape_string($b));
Then send the query to mysql_query()
to perform it:
$result = mysql_query($query);
Also note that the $link
parameter is optional. If all you ever do in that script is work with a single database connection, you may omit it to make the code clearer.
As a side note, it's bad practice to obtain the number of rows returned by a query with mysql_num_rows()
if you don't need the data as well. If all you need to find out is how many rows there are, consider using SELECT COUNT(*) AS total ...
instead, then get the value of total
with mysql_fetch_assoc()
.
And last but not least, do consider using PDO instead of the mysql_*
functions, as it will likely help you in the long run. Check out this tutorial to find out how to use it.