I am trying to connect to the MySQL service provided by XAMPP (screenshot) through a PHP script that runs on the builtin PHP server (I started the server through php -S localhost:4242
in the folder where I have the scripts). However, when I try to connect to the database, I receive this error:
Access denied for user ”@’localhost’ (using password: NO)
In my connection script, I have:
class DB
{
private $host = 'localhost';
private $user = 'root';
private $pwd = '';
private $dbname = 'mydb';
public function create_connection() {
$conn = new mysqli($host, $user, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
}
When I call create_connection()
I receive the error above. The MySQLi extension is enabled in my PHP. What am I doing wrong?
PS: I don’t want to move everything to XAMPP’s htdocs, I would like to access MySQL from the scripts outside.
Update The problem was solved. The issue was confusion between instance/class variables, and now connection works. The correct statement is:
$conn = new mysqli($this->host, $this->user, $this->pwd, $this->dbname);