0

I have a small project using PSR-4. Currently I am trying to access data from my database without much luck.

Within my root directory I have the following directory structure

/app/projectName/controller /app/projectName/model

Within my model directory I have this database file.

    <?php
namespace projectName\model;

use \PDO;
use \PDOException;

class Database
{
    public static function connect(){
        /* DB Connection*/
//Yes that is the correct port
        $dsn = 'mysql:dbname=testData;port=25060;localhost';
        $user = 'root';
        $password = 'testAccount';


        try {
            $dbc = new PDO($dsn, $user, $password);
            return $dbc;
        } catch (PDOException $e) {
            return 'Connection failed: ' . $e->getMessage();
        }
    }

}

I also have the following file within the models directory. This is the file that I am trying to access the database from.

<?php


namespace projectName\model;
use projectName\model\Database;
use \PDO;
use \PDOException;


class TestDataModel
{

    private $dbc;

    public function __construct()
    {
       $connect = new Database; 
       $this->dbc = $connect::connect();
    }

    public function getData(){

        $this->dbc->prepare("SELECT * FROM testData.users");
        $this->dbc->execute();
        $fetched=$this->dbc->fetchAll();
        return $fetched;
    }
}

Autoloading is working and if I use this code outside of the namespace within my index.php file I can access the database. The issue started when I started using PSR-4 not sure how to properly get this done.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sebastian Flores
  • 101
  • 2
  • 12

1 Answers1

-1

I wrapped my query in a try/catch and found that I was getting an authentication error.

The error I found was cause because mysql was using caching_sha2_password instead of mysql_native_password.

After running the following query I was able to connect and access data.

ALTER USER myuser IDENTIFIED WITH mysql_native_password BY 'mypassword';
Sebastian Flores
  • 101
  • 2
  • 12