简体   繁体   中英

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);
 ?>

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); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM