Learning OOP in PHP, trying to refactor my code. Can't connect to database anymore.
Posted
by Cortopasta
on Stack Overflow
See other posts from Stack Overflow
or by Cortopasta
Published on 2010-06-02T21:42:27Z
Indexed on
2010/06/02
21:44 UTC
Read the original article
Hit count: 198
Thought I understood how classes work, then I tried this code:
class user
{
var $dbcon;
var $dbinfo;
var $con;
var $error;
function dbConnect()
{
$this->dbinfo['server'] = "localhost";
$this->dbinfo['database'] = "foolish_faith";
$this->dbinfo['user'] = "user";
$this->dbinfo['password'] = "password";
$this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
$this->dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->error = $this->dbcon->errorInfo();
if ($error[0] != "")
{
print "Error!";
print_r($error);
}
}
}
Now it just spits out this error:
Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in E:\PortableApps\xampp\htdocs\dbcon.php:24 Stack trace: #0 E:\PortableApps\xampp\htdocs\dbcon.php(24): PDO->__construct('', NULL, NULL) #1 E:\PortableApps\xampp\htdocs\login.php(4): user->dbConnect() #2 {main} thrown in E:\PortableApps\xampp\htdocs\dbcon.php on line 24 Can anybody see what I'm doing wrong, as I'm sure it has to do with my lack of knowledge when it comes to classes?
© Stack Overflow or respective owner