Syd Syd - 4 months ago 29
SQL Question

Can't pass PDO Connection instance into another class instance

Hello I have a sample project I'm working on and I have created so far 5 php files.

connection_info.php -> db credentials
connection.php -> database connection
database.php -> where all wueries will be (here I pass the connection instance)
logger.php -> a simple logger
index.php -> oh well index...


Index.php

<?php

require_once 'logger.php';
require_once 'connection.php';
require_once 'database.php';

$logger = new Logger();
$connection = new Connection($connection_info['host'], $connection_info['db'], $connection_info['user'], $connection_info['pass'], $logger);
$db = new Database($connection, $logger);




?>
<!DOCTYPE html>
<html>
<head>
<title>Administration</title>
</head>
<body>

<?php

$db->authenticateUser("alex", "123456");

?>

</body>
</html>


Connection.php

<?php

require_once 'connection_info.php';

Class Connection {

private $user;
private $pass;
private $host;
private $db;
private $con;
private $logger;

function __construct($host = '127.0.0.1', $db, $user, $pass = null, &$logger) {

$this->user = $user;
$this->pass = $pass;
$this->host = $host;
$this->db = $db;
$this->logger = $logger;

try {

$connectionString = "mysql:host=$this->host;dbname=$this->db;charset=utf8";

$this->con = new PDO($connectionString, $this->user, $this->pass);
$this->logger->log('Database connection established');

return $this->con;

} catch (PDOException $e) {

$this->logger->log('Database connection failed: ' . $e->getMessage());

}

}

function __destruct() {

$this->con = null;

}

}

?>


Database.php

<?php

Class Database {

private $connection;
private $logger;

function __construct(&$connection, &$logger) {

$this->connection = $connection;
$this->logger = $logger;

}

function __destruct() {

}


public function authenticateUser($user, $pass) {

if (func_num_args() != 2) {

$this->logger('Invalid arguemnts supplied while authenticating');

return false;

} else {

try {

$query = "SELECT * FROM `users` WHERE user=:user AND pass=:pass";

$stmt = $this->connection->prepare($query); // Error occurs here
$stmt->bindValue(':user', $user, PDO::PARAM_STR);
$stmt->bindValue(':pass', md5($pass), PDO::PARAM_STR);

$stmt->execute();

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

if (count($results) == 1) {

$_SESSION['user'] = true;

return true;

} else {

$this->logger('Invalid credentials provided: "' . $user . '", "' . $pass . '"' );

return false;

}

} catch (PDOException $e) {

$this->logger('Error retrieving user: "' . $user . '", ' . $e->getMessage() );

return false;

}

}

}

}

?>


And the error I'm getting is this: "Fatal error: Call to undefined method Connection::prepare() in D:\Development\xampp\htdocs\lab\feticcio\database.php on line 34"

I'm trying to figure this out myself but my mind got stuck probably it's something silly but I could use a second pair of eyes...

Answer

Your "Connection" class has no method called "prepare". You're passing an instance of Connection into Database, and then calling a method called prepare() which doesn't exist in that class. Did you mean to call the prepare() method of the PD connection object (which is $con within your own Connection class)? You should expose that via a wrapper property (or method to directly expose the $con->prepare() method).

Comments