Search code examples
phpmysqlsqlmysqliaccess-denied

PHP using wrong password for SQL query


So I have this SQL query and when I connect the php and MySQL it works but then when I try to run it, the query has an error saying it's using the wrong username/password. But the only place I entered the username and password was when connecting, and then I used the right information and it worked? Here is the code and the error:

<?php
 $con = mysqli_connect("Correct Host","Correct Username","Correct Password","Correct Database");
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

mysql_query("SELECT username, email from Profiles");
mysqli_close($con);
?>

Error:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'wronguser'@'wronghost' (using password: NO) in /home/username/public_html/register.php on line 8


Solution

  • You've used MySQLi to connect to the database but you used MySQL to run the query. Take note, in PHP, MySQL and MySQLi are different. MySQLi is the newer and improved version of MySQL. So use mysqli_query() instead of mysql_query since you've connected to the database using MySQLi.

    Hope this helps!