Hello,
I Am having problems with a simple script i wrote to login a username and password from a mysql database.
i get the error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in H:\steadyology\steadyology\account.php on line **21

I ran the script on my localhost where i have php and mysql running. the following is the query i perfomrmed on the datbase table user

<?php 
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
                                                     // always modified
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");                          // HTTP/1.0




include("config.php");


 $user=$_POST["user"]; $user=strtoupper($user);
 $pass=$_POST["pass"];
$query="select password from user where user=$user";

$result=mysql_query("select * from user where user=$user",$db);

if(!($arr=mysql_fetch_array($result))){//rest that username exists

   include("loginerror.htm");
    exit;
 }

 $done=strcmp($arr["password"],$pass);
if(!($done==0)){  // test that password is correct

    include("loginerror.htm");
    exit;

The line number with error is ****if(!($arr=mysql_fetch_array($result)))

the user table has the following fields
user_id (primary key)
user (contains the username)
pass (the password)

Any help will be appreciated.

Thank you
**

Dani AI

Generated

Quick diagnosis: that warning means the call that should return a MySQL result set actually returned false, so mysql_fetch_array() received a boolean instead of a resource. Common immediate causes are: an SQL syntax error (often from not quoting a string), a bad/absent DB connection resource, or mismatched column/table names. As suggested, printing the final SQL and the DB error will reveal which of those happened; as noted, check row count before fetching.

Immediate troubleshooting checklist (do these in order):

  • Confirm the DB connection variable from config.php is a valid resource.
  • Echo the final query and run mysql_error() after mysql_query() to see the exact SQL error.
  • Make sure string values are quoted and escaped (use mysql_real_escape_string) — unquoted strings cause syntax errors.
  • Verify actual column names: if the table column is pass, do not read password from the result.
  • Use mysql_num_rows() before calling mysql_fetch_*() to avoid fetching from a failed/empty result.

Example fix (debug-friendly, not identical to the original snippet):

$user = mysql_real_escape_string($_POST['user']);
$sql  = "SELECT `pass` FROM `users` WHERE `username` = '$user' LIMIT 1";
$res  = mysql_query($sql, $db) or die('DB error: ' . mysql_error());

if (mysql_num_rows($res) === 0) {
    include('loginerror.htm');
    exit;
}

$row = mysql_fetch_assoc($res);
if ($row['pass'] !== $_POST['pass']) {
    include('loginerror.htm');
    exit;
}

Notes and best practices: avoid using column/table names that clash with system tables or keywords (rename userusers, user column → username). Do not rely on strtoupper() unless the DB stores usernames in uppercase — use case-insensitive comparison or normalize both sides. For production, migrate away from the old mysql_* API to PDO or MySQLi with prepared statements and store passwords with password_hash()/password_verify() to prevent SQL injection and improve security.

Recommended Answers

All 2 Replies

1) Echo out your query to the screen. There is an error in it somewhere and that's the best way to find it.

2) Use [ code][/code ] tags when displaying your code. It makes it easier for us to read. :)

The line number with error is if(!($arr=mysql_fetch_array($result)))

Without seeing the actual error output, I would have to assume your error is being caused where you are placing your !($arr ..

Try this usage instead:

$result = mysql_query(.............);
if (mysql_num_rows($result) > 0)
{
    // we do indeed have results.. collect the array
    $arr = mysql_fetch_array($result);
}
else
{
    // do something here if we don't have any rows returned
}

Regards,
Peter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.