Good afternoon, figuring out the pdo , I decided that it is more logical to use pdo , I want to translate my simple form of authorization from mysqli to pdo , and problems literally immediately fell down (I left the commented out code with mysqli )

connection:

  require 'app_config.php'; //$mysql=mysqli_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD) or die("<p>Ошибка к подключению БД :" . // mysqli_error($mysql) . "</p>"); //mysqli_select_db($mysql, DATABASE_NAME) or die("<p>Ошибка при выборе БД</p>" . mysqli_error($mysql) ); //Через pdo DATABASE HANDLE //$DBH = new PDO("mysql:host=DATABASE_HOST;dbname=DATABASE_USERNAME", DATABASE_NAME,DATABASE_PASSWORD); $DBH = new PDO('mysql:dbname=ch33404_testdb;host=localhost', 'DATABASE_USERNAME', 'DATABASE_PASSWORD'); $DBH - > setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

like this gives more errors, I don’t understand why:

// $ DBH = new PDO ("mysql: host = DATABASE_HOST; dbname = DATABASE_USERNAME", DATABASE_NAME, DATABASE_PASSWORD);

Essence: there is a simple form with fields, the create_user.php script should work out and write the user to the database, now after clicking "login", this error takes off


Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [HY000] [1045] Access denied for user' DATABASE_USERNAME '@' localhost '(using password: YES)' in / home / c / ch33404 / testsite / public_html / test / scripts / database_connection.php: 15 Stack trace: # 0 /home/c/ch33404/testsite/public_html/test/scripts/database_connection.php(15): PDO -> __ construct ('mysql: dbname = ch ...', 'DATABASE_USERNA ...', 'DATABASE_PASSWO ...') # 1 /home/c/ch33404/testsite/public_html/test/scripts/create_user.php(3): require ('/ home / c / ch33404 ... ') # 2 {main} thrown in /home/c/ch33404/testsite/public_html/test/scripts/database_connection.php on line 15


Here is the script create_user.php:

 <?php require'database_connection.php'; $first_name=trim($_REQUEST['first_name']); $last_name=trim($_REQUEST['last_name']); $email=trim($_REQUEST['email']); $bio=trim($_REQUEST['bio']); $vkontakte_url=str_replace("vk.com", "vkontakte.com", trim($_REQUEST['vkontakte_url'])); $position=strpos($vkontakte_url, "vk.com"); if ($position===false) { $vkontakte_url="http://www.vk.com/" . $vkontakte_url; } $twitter_handle=trim($_REQUEST['twitter_handle']); $twitter_url="http://www.twitter.com/"; $position=strpos($twitter_handle, "@"); if ($position===false) { $twitter_url=$twitter_url . $twitter_handle; } else { $twitter_url=$twitter_url . substr($twitter_handle, $position + 1); } ////через mysqli //$insert_sql = "INSERT INTO users (first_name, last_name, email, bio, vkontakte_url, twitter_handle) " . "VALUES ('{$first_name}', '{$last_name}', '{$email}', '{$bio}' " . // "'{$vkontakte_url}', '{$twitter_handle}');"; // //Добавить пользователя в базу данных //mysqli_query($mysql,$insert_sql) or die(mysqli_error($mysql)); //Вставка данных через PDO $STH=$DBH->prepare("INSERT INTO users ( first_name, last_name,email, bio, vkontakte_url, twitter_handle) values ( '{$first_name}', '{$last_name}', '{$email}', '{$bio}', '{$vkontakte_url}', '{$twitter_handle}' )"); $STH->execute(); //Отправляем пользователя на свой профиль //header("Location: show_user.php?user_id=" . mysqli_insert_id($mysql)); 

in line 10 connection.php is:

$ DBH = new PDO ('mysql: dbname = ch33404_testdb; host = localhost', 'DATABASE_USERNAME', 'DATABASE_PASSWORD');

  • 2
    From the error it is clear that access is denied to the user. Most likely the username or password from the database was incorrectly entered in the 10 line connection.php - UserX
  • a little to the side: $DBH - > - $DBH -> (there should be no space in the arrow) - Alex

1 answer 1

Each PCP user, even without being a programmer, should develop the habit of reading error messages. And - not least - read the whole, not just the first few words.

Usually the error message says in detail what the problem is.

For example, in this case, we are told that the user is trying to log in with the name DATABASE_USERNAME, which looks rather unusual, and more like the name of a constant than the name of the database.

We look at the calling code and make sure that yes - for some reason, the constant is enclosed in quotes, and as a result, not its value is transferred, but literally the string DATABASE_USERNAME.

If you remove the quotes, then the connection to the PDO will be successful.

To use constants in the DSN string, concatenation should be used:

 $dsn = 'mysql:dbname='.DATABASE_NAME.';host='.DATABASE_HOST 

And once again I remind you of the need to write out correct and actual values ​​in the above constants.