开发者

How can I solve this mysql_num_rows error

This is the code I am working with

<?php
$link = mysql_connect('localhost', 'root', '')
    OR die(mysql_error());

          $user =  $_POST['username'];
      $password = $_POST['password'];

$login = sprintf("SELECT * FROM imagehosting WHERE username='%s' AND password='%s'  ",
            mysql_real_escape_string($user, $link),
            mysql_real_escape_string($password, $link));

      开发者_JS百科      $rowcount = mysql_num_rows($login);
            echo "<br /><br />" . $rowcount; 
?>

This is the error I am getting

Warning: mysql_num_rows() expects parameter 1 to be resource

I understand that I should use mysqli, but the php.net page used mysql, so I am just trying to learn how to use the $printf and $mysql_num_row.

I am not 100% sure what I am doing with this $sprintf, so I'm sorry if the question is too basic.


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.


You can't obtain the number of rows in a recordset from just a string. You have to select a database, and actually execute the query.

The manual page for mysql_num_rows (which of course you read before using the function!) has a handy example of exactly what you need to do.

Here's their example:

<?php

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

This — with modifications to specific parameter values — is the minimum required to make this DB interface work.


I think you forgot to tell the server which database to work on. Can you try this one:

<?php
$link = mysql_connect('localhost', 'root', '') 
    OR die(mysql_error());

$db = mysql_select_db('mydb', $link)

$user =  $_POST['username'];
$password = $_POST['password'];

// ...and so on, and so forth
?>


Try this...

I add you select DB, query execution and correct num_rows select.

<?php
$link = mysql_connect('localhost', 'root', '') OR die(mysql_error());
mysql_select_db('your_db',$link);

$user =  $_POST['username'];
$password = $_POST['password'];

$login = sprintf("SELECT * FROM imagehosting WHERE username='%s' AND password='%s'  ",
            mysql_real_escape_string($user, $link),
            mysql_real_escape_string($password, $link));
$query=mysql_query($login) OR die(mysql_error());

            $rowcount = mysql_num_rows($query);
            echo "<br /><br />" . $rowcount; 
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜