It is possible to access the mysql database using the terminal, but I get an "access denied" message when using php

When I log into mysql in my terminal, everything works. But when I run my webpage with

$conn = new mysqli("localhost", "root", "password");
if($conn->connect_error) {
   die("connection failed: " . $conn->connect_error);
}
echo "connected successfully";

      

I get "Connection failed: access denied for user" root "@" localhost "(using password: YES)". I tried to reset the root password. I tried giving all permissions to root. I am absolutely sure that I am entering my username and password correctly in the php file.

I am running a LAMP server on an Ubuntu 14.04 machine. I have php 5.5.9, mysql 5.5.40 and apache 2.4.7.

Can anyone please help?

+3


source to share


2 answers


PHP requires you to fill in the username, password, and database name for the database you are trying to connect to: http://php.net/manual/en/function.mysql-connect.php



$conn = new mysqli("localhost", "root", "password", "database");

      

+2


source


Try mysqli_connect_errno()

$conn = new mysqli("localhost", "root", "password");
if(mysqli_connect_error()) {
   die("connection failed: " . mysqli_connect_error());
}
echo "connected successfully";

      

Ordering Guide:



connect_error

Warning The mysqli->connect_error property only works properly as of PHP versions 5.2.9 and 5.3.0. Use the mysqli_connect_error() function if compatibility with earlier PHP versions is required.

0


source







All Articles