user17289472
user17289472

Reputation: 41

trouble connecting to mysql with pdo

I am trying to connect to MySQL database with pdo but keep getting this error:

Warning: Use of undefined constant username - assumed 'username' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\first\index.php on line 2

Warning: Use of undefined constant password - assumed 'password' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\first\index.php on line 2

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES) in C:\xampp\htdocs\first\index.php:2 Stack trace: #0 C:\xampp\htdocs\first\index.php(2): PDO->__construct('mysql:host=loca...', 'username', 'password') #1 {main} thrown in C:\xampp\htdocs\first\index.php on line 2

here's my code:

<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=dbname', username, password);
 ?>

Upvotes: 1

Views: 1205

Answers (1)

joeb
joeb

Reputation: 877

It's because the username and password are not variables. They are missing the $

<?php

$username = 'myUsername';
$password = 'myPassword';

$pdo = new PDO('mysql:host=localhost;port=3306;dbname=dbname', $username, $password);
 ?>

Upvotes: 0

Related Questions