Having trouble with PHP/MySQL

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 require "templates/header.php"; ?> <?php if (isset($_POST['submit']) && $statement) { ?>
<?php echo $_POST['firstname']; ?> successfully added.
<?php } ?>

Add a user

First Name Last Name Email Address Age Location

Back to home

<?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.

I dont think anyone here is going to try and debug third party tutorial code. I would suggest you start a new copy from scratch and carefully go through the tutorial again. Once you have the new one working, do a file compare to find out what you did wrong in the first one.

While it is great you found a tutorial that uses PDO there are still some minor issues with the code that could be improved, but they are not things that will break it.

I believe it was the connection part but am not sure what’s the issue. Is there a way I could find out my login details for the SQL system (I believe I have mysqli or something like that that I downloaded.)

I looked up W3 schools and found that you need to download PDO or something. I think I downloaded mysqli but not PDO. Could that be an issue?

Wait, they said it should be already there by default.

Run a phpinfo script on your server. It will show you if PDO is there. You also need to turn on error reporting and check the server error log.

How do you turn on error reporting? Also, PDO is there. However, it has listed mysql and not mysqli under the PDO. However, mysql turned up in another spot in the phpinfo and was listed as enabled.

I found something to enable error reporting, but maybe I put it in the wrong file. Anyway, now I got something.

Notice : Undefined variable: sql in C:\inetpub\wwwroot\Database Test\install.php on line 28

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
Warning : require(…/common.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\Database Test\create.php on line 16

Fatal error : require(): Failed opening required ‘…/common.php’ (include_path=’.;C:\php\pear’) in C:\inetpub\wwwroot\Database Test\create.php on line 16

common.php exists, though.

Updated, fixed the error that was causing sql to be undefined.

Still getting:

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
Warning : require(…/common.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\Database Test\create.php on line 16

Fatal error : require(): Failed opening required ‘…/common.php’ (include_path=’.;C:\php\pear’) in C:\inetpub\wwwroot\Database Test\create.php on line 16

Good job getting error reporting on. FYI: There is no tripple dot slash to go up the directory tree.

I tried the other method, the mysqli, and got a similar problem.

$localhost =“localhost”;

// Create connection
$conn = mysqli_connect($localhost, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo “Connected successfully”;

Warning : mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in C:\inetpub\wwwroot\Database Test\install.php on line 22

I think I may be giving it bad connection credentials.

However, I’m not getting any errors like those listed here:

First. Stop bouncing around. Stick to PDO. This tutorial will get you going.

I’m still not understanding the error. NOTHING is working.

A solution I keep seeing appear over and over to people who have this error is:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password
BY 'password';  

But how do I put that in?

Just forget about hacks and workarounds and use PDO like I told you.

I am. Still getting this.

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

If I put the code from config and install together, it looks like this, and it uses PDO:

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

try {

$connection = new PDO(“mysql:host=$host”, $username, $password, $options);
$sql = file_get_contents(“data/init.sql”);
$connection->exec($sql);

echo “Database and table users created successfully.”;
} catch(PDOException $error) {
echo $error->getMessage();

}

It is running into trouble when I create the PDO object and outputting $error->getMessage().

Doesn’t seem to matter if I switch the username or password (between the one the example gave and what I think my root one has.) Goes to the same error both times.

That file_get_contents routine is pretty hokey.

Just download my PDO Bumbstart Database. I wrote it specifically to help newbies get going.

1 Like

The table actually already exists. I ran the SQL script on my own.
I have PHP Version 7.1.7

Also, the file_get_contents() isn’t what’s causing it, it’s the PDO object itself.

The connection error is because you are using an incompatible authentication algorithm between the php driver and the database server. Since you are using a later php version, that would lean toward an out of date database version. What is the database server type and version and how did you obtain/install both the web server/php and the database server? Is this a recent install of the web server/php and the database server or has there been a number of upgrades to either or both?

1 Like

I’ve had PHP installed for at least a year. However, the SQL database has been up for a week, if that.

Run the alter SQL you posted. There is a “bug” with Php and Mysql 8 that will be fixed in php 7.4

1 Like

Is PHP 7.4 out yet? And where do I run the alter at, in the SQL command prompt, in the data.sql file, or somewhere else?

Run it from the command line, phpmyadmin or any other mysql gui.

1 Like

I ran this and it still has the error.

The posted code isn’t using the root user, so the ALTER statement isn’t affecting the correct user. You would need to ALTER the the setting for the mongoose username.

However, see the following from the php.net PDO_MySQL documentation -

This is because MySQL 8 defaults to caching_sha2_password, a plugin that is not recognized by the older PHP (mysqlnd) releases. Instead, change it by setting default_authentication_plugin=mysql_native_password in my.cnf.

So, find the my.cnf for your database server and add/edit a line setting the default_authentication… Stop and start your database server to get any change made the my.cnf to take effect.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service