I am trying to create a REST interface in php using a tutorial (https://www.techiediaries.com/php-rest-api/) that fetches data in a MYSQL database. An error "The server requested authentication method unknown to the client" is thrown.
Error message on page http://127.0.0.1/rest/products/read.php :
Error: SQLSTATE[HY000] [2054] The server requested authentication method unknown to the clientSELECT c.name as family_name, p.id, p.sku, p.barcode, p.name, p.price, p.unit, p.quantity , p.minquantity, p.createdAt, p.updatedAt FROM Product p LEFT JOIN Family c ON p.family_id = c.id ORDER BY p.createdAt DESC<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\rest\entities\product.php:36
Stack trace:
#0 C:\xampp\htdocs\rest\products\read.php(12): Product->read()
#1 {main}
thrown in <b>C:\xampp\htdocs\rest\entities\product.php</b> on line <b>36</b><br />
Usually the (or at least a similar) bug (in https://www.php.net/manual/de/ref.pdo-mysql.php) is well known, but not for my version.
server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.2
Mysql-Server: 8.0.14 (determined by select version ();
)
Changing the my.ini did not solve the problem:
[mysqld]
default-authentication-plugin=mysql_native_password
fault location -> rest\entities\product.php:
// Connection instance
private $connection;
// table name
private $table_name = "Product";
// table columns
public $id;
public $sku;
public $barcode;
public $name;
public $price;
public $unit;
public $quantity;
public $minquantity;
public $createdAt;
public $updatedAt;
public $family_id;
public $location_id;
public function __construct($connection){
$this->connection = $connection;
}
//C
public function create(){
}
//R
public function read(){
$query = "SELECT c.name as family_name, p.id, p.sku, p.barcode, p.name, p.price, p.unit, p.quantity , p.minquantity, p.createdAt, p.updatedAt FROM " . $this->table_name . " p LEFT JOIN Family c ON p.family_id = c.id ORDER BY p.createdAt DESC";
echo $query;
//Here is the error:
$stmt = $this->connection->prepare($query);
$stmt->execute();
return $stmt;
}
//U
public function update(){}
//D
public function delete(){}
}
dbclass.php:
<?php
class DBClass {
private $host = "localhost";
private $username = "root";
private $password = "mypw";
private $database = "RESTSCHNITTSTELLE";
public $connection;
// get the database connection
public function getConnection(){
$this->connection = null;
try{
$this->connection = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->database, $this->username, $this->password);
$this->connection->exec("set names utf8");
}catch(PDOException $exception){
echo "Error: " . $exception->getMessage();
}
return $this->connection;
}
}
?>