MySQL Warning Error


#1

Im making a script, and it envolves mysql (obviously) and im just trying to check some things, and it gives me this error numerously.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/oddlaw/public_html/brstest/members.php on line 42

Line 42 is basically this :

       while ($row = mysql_fetch_array($query)){

Here are some lines around it :

      $_connection = mysql_connect("localhost", "oddlaw_mem", "ssjgoku");

      mysql_select_db("oddlaws_members", $_connection);

       $query = mysql_query("SELECT * FROM idnumber");

        while ($row = mysql_fetch_array($query)){
               $pass = $row["id"];

             if($pass == $_POST['password']) {

       $loggedin = TRUE;

          break; //stop looping if the password is found
          } else {
 print " 
 <a href="#" onClick="history.go(-1)">Wrong unique ID number,
  go back and try again!</a>";
 } //end if

    }// end while

Well if anyone can help me on this, then please it would be much appreciated!
If anything i would just like to know what the supplied argument warning means.


#2

Ok see a couple of problems. You have no way of handling database errors or being told about them. All these tips are good practice. for a little bit of extra code you get a more flexible and robust program.

[php]
/* your line has no way to tell you if you can’t connect to the database. Just because it should doesn’t always mean it will.*/
$_connection = mysql_connect(“localhost”, “oddlaw_mem”, “ssjgoku”);

/* Instead do this. It tells the program to stop gracefully if there is a problem connecting. All the mysql_error() should be removed in the production program since the user has no need to see this debugging information (not too mention they may be able to use it to hack into your system)*/
$_connection = mysql_connect(“localhost”, “oddlaw_mem”, “ssjgoku”) or die (“Error connecting to the database” . mysql_error());

/* Again there is nothing here to handle it if you don’t select the database*/
mysql_select_db(“oddlaws_members”, $_connection);

/* Instead do this for all the reasons given above*/
mysql_select_db(“oddlaws_members”, $_connection) or die (“Error connecting to the database” . mysql_error());

/* This in and of itself has no problems, but what if the query doesn’t run? You should always have something to handle that */
$query = mysql_query(“SELECT * FROM idnumber”);

/* I suggest something like this */
if (!$query) // if there is something wrong
{
echo 'Error: There is a problem with the query. ';
// this is another mysql_error() that will need to be removed or
// commented out in production. This is more for debugging purposes
echo mysql_error() . ‘
’;
exit; // stop the page or you could redirect somewhere
}

while ($row = mysql_fetch_array($query)){
[/php]