-2

I am currently getting this error when using mysql_fetch_assoc($query);

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a6461923/public_html/index.html on line 111

Here is my query:

$query = mysql_query("SELECT * FROM users WHERE email='$email' AND password='$encripted_password'");

Thanks!

3
  • You should be using mysqli_* or PDOs.
    – Jared
    Commented Jun 1, 2012 at 1:26
  • Please use the search function. This question has been answer time and time again.
    – jprofitt
    Commented Jun 1, 2012 at 1:28
  • Your query is vulnerable to SQL injections. Read up at en.wikipedia.org/wiki/SQL_injection
    – dvir
    Commented Jun 1, 2012 at 1:29

4 Answers 4

6

My query is not working…

Steps before asking this question (yet again):

  1. Verify your connection to the database.
  2. Ensure there is not an error in your query.

You can do this by:

  • Connecting to your database outside of PHP with the same credentials used in your code.
  • Outputting your query (echo $query;) then running it against the database directly (such as through PHPMyAdmin or mysql CLI):
  • Use functions like mysqli_error() and mysqli_num_rows() in your code.

Some additional tips about common PHP/MySQL programming:

  • Read up on SQL Injection.
  • Familiarize yourself on when to escape and quote literal values in MySQL.
  • Be mindful not to prematurely close your MySQL connection. PHP will actually do so automatically at the end of the script.
  • As commented by Jared, You should use MySQLi or PDO, instead of the now deprecated MySQL extension.
  • Also look into Prepared Statements.
0
1

This probably means the query in mysql_query() went wrong. PHP says the error is in mysql_fetch_assoc() because there is where it tries to read the query's result. And if the result is not a valid resultset, then a Warning is thrown.

Try something like die($query); after you build up the $query string to see if anything is wrong (missing variables, happens for everyone once).

0

I believe you need to run something like the following:

$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

See this link for a more complete example.

0
0

Please read the manual. mysql_fetch_assoc does not take a string argument. It takes a resource which you get from passing a query to mysql_query.

https://www.php.net/manual/en/function.mysql-query.php

http://php.net/manual/en/function.mysql-fetch-assoc.php

Not the answer you're looking for? Browse other questions tagged or ask your own question.