Adrià Adrià - 5 months ago 21
MySQL Question

PHP mysql_num_rows() not working as expected

I've got a mysql database with absolutely no values or rows, and when I call mysql_num_rows(), it returns like the table has got the row.

I've tried everything I could thought about, but didn't work.
I post the code:

function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
}
...
$name = test_input($_POST["name"]);
$lname = test_input($_POST["lname"]);
$email = test_input($_POST["email"]);
$uname = test_input($_POST["uname"]);
$password = test_input($_POST["password"]);
$password = md5($password);

$q2 = mysql_query("SELECT * FROM `users` WHERE `uname` = '$uname'");
if(mysql_num_rows($q2) > 0){
echo "<p>Sorry, that username already exists</p>";
}
...

Answer

Return from test_input function

  function test_input($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

Also do mind as terry commented. don't use md5 for password and use pdo or mysqli for db connection.

Comments