0

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-&gt;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;
    }
}
?>
Josef Soe
  • 91
  • 8
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Feb 24 '20 at 22:46
  • 1
    `Call to a member function prepare() on null` ...this means that `$this->connection` was null. So you presumably didn't pass a valid connection object when you instantiated the `product` class. But we can't see what code you used to do that, so it's hard to know exactly what has happened. – ADyson Feb 24 '20 at 22:55

0 Answers0