PHP_Ninja PHP_Ninja - 1 year ago 220
PHP Question

PHP PDO SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'events'

When I connect to the database using MySQLi, everything works as it should, but with PDO or with CI database methods, I get this error : SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'events'.

try {
$link = new PDO("mysql:host=localhost;dbname=events;");

//create statement;
$statement = $link->prepare($sql);
$statement->bindParam(":username", $login_request["username"]);
$statement->bindParam(":password", $login_request["password"]);

$rows = $statement->fetchColumn();

return ($rows == 1) ? true : false;
echo $e->getMessage();

Answer Source

Try to connect like this. Put it in a separate file connection.php and include it in every page you run queries

$connect = new PDO('mysql:host=localhost;dbname=YOUR_DB_NAME;charset=utf8', 'USERNAME', 'PASSWORD',
}catch(PDOException $e){
    die('Error connecting to database');

then you can run queries in every page like this

$stmt = $connect->prepare("SELECT * FROM table WHERE id=:id");
$stmt->bindParam(':id', $id);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download