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.