Search code examples
phpmysqlmysql-num-rows

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));

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


Solution

  • 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; 
    ?>