I tried following the example here:
I know where it’s going wrong but am not fully certain why:
create.php
<?php
/**
- Use an HTML form to create a new entry in the
- users table.
*/
if (isset($_POST[‘submit’])) {
require “./install.php”;
// require “…/config.php”;
require “…/common.php”;
try {
$connection = new PDO($dsn, $username, $password, $options);
echo “It died here.”;
$new_user = array(
“firstname” => $_POST[‘firstname’],
“lastname” => $_POST[‘lastname’],
“email” => $_POST[‘email’],
“age” => $_POST[‘age’],
“location” => $_POST[‘location’]
);
$sql = sprintf(
“INSERT INTO %s (%s) values (%s)”,
“users”,
implode(", “, array_keys($new_user)),
“:” . implode(”, :", array_keys($new_user))
);
$statement = $connection->prepare($sql);
$statement->execute($new_user);
} catch(PDOException $error) {
echo $sql . “
” . $error->getMessage();
}
}
?>
<?php echo $_POST['firstname']; ?> successfully added.<?php } ?>
Add a user
First Name Last Name Email Address Age Location <?php require "templates/footer.php"; ?>install.php
<?php
/**
* Open a connection via PDO to create a
* new database and table with structure.
*
*/
require "config.php";
try {
$connection = new PDO("mysql:host=$host", $username, $password, $options); // this line appears to be the problem.
$sql = file_get_contents("data/init.sql");
$connection->exec($sql);
echo "Database and table users created successfully.";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
config.php
<?php
/**
* Configuration for database connection
*
*/
$host = "localhost";
//$username = "mongoose";
$username = "root";
$password = "Melissa89";
//$password = "root";
$dbname = "test"; // will use later
$dsn = "mysql:host=$host;dbname=$dbname"; // will use later
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
I believe the problem is somewhere in here:
$connection = new PDO("mysql:host=$host", $username, $password, $options); // this line appears to be the problem.
$sql = file_get_contents("data/init.sql");
$connection->exec($sql);
It keeps returning SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
which is what the catch block throws.
I know I have mysql installed and php installed so that’s not the issue. It could be that for some reason it’s not giving the correction credentials to my SQL system. I have tried both the login for the one they gave and what should be my login and password for my system. It’s possible that I’m getting it wrong.
The problem is definitely on the SQL and not the PHP end but beyond that, I’m not sure what the issue is for certain. (I’ve looked up that error code and some have suggested downgrading the SQL version, for instance, so it’s not a given that it’s an authentication problem, but it probably is.)
PS, I tried to find a [code[ button to make the code look pretty but the preformatting thing was the closest thing to it that I could find.
Interestingly, their example ran install.php some other way rather than the include but I wasn’t sure how they did it so I did the include (before I had it, it was simply having the page not load properly so I know I needed it but wish there was some way I could run it once without having to run it every time.)
I believe it’s happening before I go to the init.sql file, which has the code:
CREATE DATABASE test;
use test;
CREATE TABLE users (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
age INT(3),
location VARCHAR(50),
date TIMESTAMP
);
However, I could be wrong.
My best guess is that it’s happening with
$connection = new PDO("mysql:host=$host", $username, $password, $options);
based on how echo statements I put in to test it were running.
However, mysteriously it also ran an echo statement that was AFTER the require of install.php but before the require of common.php
So when I that echo statement in, it would go to the thing in the catch block, echo that, then echo the thing between the two requires and then stop after that. (As it didn’t run anything I put in or after common.php.)
I’m not sure why that happened.