I am trying to search for something in the database. However, this error keeps popping up. I was wondering if anyone could help me. Thanks

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
$host = '127.0.0.1';
$database = 'lab11';
$conn = mysql_connect($host);
$select = mysql_select_db($database, $conn);
$user = $_POST['user'];
$pass = $_POST['pass'];
if (! $conn)
die("Couldn't connect to MySQL");
$query = 'SELECT * FROM `security` LIMIT 0 , 30';
$result = mysql_query($query, $conn);
$row = mysql_fetch_assoc($result);
$a = array_search($user, $row);
echo $a;

mysql_close($conn);
echo 'Hi ';
echo $user. '.' ;
echo ' I can see your password.';
echo ' It is ';
echo $pass;
?>
</body>
</html>

Dani AI

Generated

The warning means mysql_fetch_assoc() was given something other than a valid MySQL result resource — most often because mysql_query() failed and returned FALSE, or because the variable holding the result was overwritten or never set. was on the right track with basic checks; the most reliable quick test is to check the return value of the query and read the MySQL error message so the real cause (syntax, wrong DB/table, permissions, etc.) is visible.

A concise, fail-fast check (different from the snippets already posted) looks like this:

$result = mysql_query($query, $conn);
if ($result === false) {
    die('MySQL query failed: ' . mysql_error());
}
$row = mysql_fetch_assoc($result);

Additional practical checks that often find the problem:

  • Confirm $conn is a valid resource and that mysql_select_db() returned true.
  • Turn on full error reporting during debugging:
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
  • Avoid PHP short tags (<?) — use <?php so the code actually runs on servers where short tags are disabled.
  • Make sure the table name is correct and not a reserved word, and confirm the query runs in phpMyAdmin with the same DB/user.

Because the posted code echoes plaintext passwords and uses deprecated mysql_* calls, a safer modern pattern is to use mysqli or PDO with prepared statements and hashed passwords (password_hash / password_verify). A minimal mysqli example (structure only) shows the safer flow: prepare, bind, execute, fetch, and verify — never echo stored passwords. Also prefer checking mysql_errno() / server error logs for transient permission or connection issues rather than exposing raw errors in production.

Finally, instead of using array_search on a fetched row, reference columns by name ($row['username'], $row['password_hash']) so the intent and logic stay clear and robust. reported the original problem was resolved after checks; keeping the above practices will prevent the same warning and improve security.

Recommended Answers

All 5 Replies

Hi there,
Have you tried running your sql statement straight through your SQL command interface (like phpMyAdmin). If that returns no errors, try adding some error catching as follows and check the results:

$query = 'SELECT * FROM `security` LIMIT 0 , 30';
$result = mysql_query($query, $conn);
if (mysql_numrows($result) > 0)
{
$row = mysql_fetch_assoc($result);
$a = array_search($user, $row);
echo $a;
} else {
echo "No rows returned";
}

Thanks for the help but the same error appears and the query works in sql. Is there any more actions that you can recommend me to fix this error?

Try this, it seems that the $result variable is set to NULL when you try and retrieve rows from it, if you are sure that your sql is correct, are you certain that you are connecting to your database correctly? I notice that you aren't passing a username or password to your mysql_connect() statement. But if that is also fine then try this:

$query = 'SELECT * FROM `security` LIMIT 0 , 30';
$result = mysql_query($query, $conn);
if (mysql_numrows($result) == 0)
{
echo "Result set is EMPTY";
} else if (!$result) {
echo "Result set is NULL";
} else {
echo "Result set is VALID";
$row = mysql_fetch_assoc($result);
$a = array_search($user, $row);
echo $a;
}

Apart from that, I'm pretty much out of ideas, hope this helps

Thanks for the help!!!
It worked finally. You were a great help

Swt, anytime

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.