• PHP Help PHP Coding
  • Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

I'm only 12 years old so don't use too much fancy words! XD

Anyways i'm trying to code a PHP register and login system intergrated with Flash for an MMO Virtual World i'm working on. But I keep getting the error above. :mad:

I signed up for PHP Builder hoping you guys can help me fix it! 🙂

I get this error specifically;

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/u660040034/public_html/data/register.php on line 10

This is my register.php code:

<?php /** REGISTRATION SCRIPT **/
require_once('../include/dbconnect.php');//require DB Connection
$user = $_GET['user']; //Method 'GET' retrieves the variables from flash
$pass = $_GET['pass']; //retrieve the variables from flash

//Query the databse for the user and pass combo
$result = mysql_query("SELECT * FROM accounts WHERE user = '$user'");

//if the rows returned from the query are over 0 (the username already exists)
if(mysql_num_rows($result) > 0){
    //send flash the error to be displayed.
    $register = "&err=Registration failed."; //'err' is the variable in flash for storing errors/succes
    echo($register);
} else {
    //If the username does not already exist insert the data into the database
    mysql_query("INSERT INTO accounts (id,user,pass) VALUES ('','$user','$pass')");
    //send flash the success message so the user may login
    $register = "&err=Registration successful.";
    echo($register);
}
?>

Then in my login code i get the same error, on the same line. :queasy:

<?php /** LOGIN SCRIPT **/
require_once('../include/dbconnect.php'); //require DB Connection
$user = $_GET['user']; //Method 'GET' retrieves the variables from flash
$pass = $_GET['pass']; //retrieve the variables from flash

//Query the databse for the user and pass combo
$result=mysql_query("SELECT * FROM accounts WHERE user = '$user' AND pass = '$pass'");

//if the rows returned from the query are over 0 (the username already exists)
if(mysql_num_rows($result) == 0){
//send flash the error to be displayed.
$login = "&err=Login failed."; //'err' is the variable in flash for storing errors/succes
echo($login);
} else {
//If the username does not already exist insert the data into the database
$row = mysql_fetch_array($result);
$user=$row['user'];
$pass=$row['pass'];
//send flash the success message along with the user and pass
$login = "&user=" . $user . "&pass=" . $pass . "&err=Login Successful.";
echo($login); 
}
?>

Help me PHP Builder!

    I would:

    • Check that there is a working connection to the database server.

    • Check that the correct database was selected.

    • Check that the SQL statement is correct. (Does the accounts table even exist with a column named user?)

    Also, you should properly escape your input. In this case the input is a string and you are using the MySQL extension, so use [man]mysql_real_escape_string/man. (Hint: switch to the MySQLi or PDO extensions.)

      I think my SQL syntax is wrong, does this one sound right?:
      $result=mysql_query("SELECT * FROM accounts WHERE user = '$user' AND pass = '$pass'");

        Or should I use this one:
        '$result=mysql_query(&quot;SELECT * FROM accounts WHERE user = '$user' AND pass = '$pa'

        I'm a PHP newbie. /:

          Syntactically, they both may be correct, so you need to do things like use [man]mysql_error/man to give you more information.

            Ok how would I add mysql error(), can you give me an example?

              I linked to the PHP manual; examples are available there.

                Actually nevermind, I would add it like this:

                <?php
                $mysql_host = "host";
                $mysql_database = "db";
                $mysql_user = "user";
                $mysql_password = "pass";
                
                $link = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password");
                
                mysql_select_db("nonexistentdb", $link);
                echo mysql_errno($link) . ": " . mysql_error($link). "\n";
                
                mysql_select_db("kossu", $link);
                mysql_query("SELECT * FROM nonexistenttable", $link);
                echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
                ?>

                So should I just stick it in at the end of my code and replace some of the parts with my database information.

                  Write a Reply...