Welcome to WebmasterWorld Guest from 54.196.244.206

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PDO database query problem

Call to a member function prepare() on a non-object

     
12:32 pm on Feb 16, 2014 (gmt 0)

Junior Member

joined:Nov 5, 2011
posts: 55
votes: 0


Hi lads, i have small problem with PDO connection. I use Xammp, and i have PDO extension loaded. I start to learn object programming in PHP and i have some problem's with this code:

This is my class (database.class.php) script:

<?php
class Database{
private $host = 'DB_HOST';
private $user = 'DB_USER';
private $pass = 'DB_PASS';
private $dbname = 'DB_NAME';

private $dbh;
private $error;
private $stmt;

public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}

public function query($query){
$this->stmt = $this->dbh->prepare($query);
}

public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}

public function execute(){
return $this->stmt->execute();
}

public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

public function rowCount(){
return $this->stmt->rowCount();
}

public function lastInsertId(){
return $this->dbh->lastInsertId();
}

public function beginTransaction(){
return $this->dbh->beginTransaction();
}

public function endTransaction(){
return $this->dbh->commit();
}

public function cancelTransaction(){
return $this->dbh->rollBack();
}

public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
?>


This script (index.php) should put some values into database:

<?php
// Include database class
include 'database.class.php';
define("DB_HOST", "localhost");
define("DB_USER", "someuser");
define("DB_PASS", "somepass");
define("DB_NAME", "somedbname");
//
$database = new Database();
$database->query('INSERT INTO mytable (username, name, secname, email) VALUES (:uname, :iname, :nname, :ename)');
$database->bind(':uname', 'value1');
$database->bind(':iname', 'value2');
$database->bind(':nname', 'value3');
$database->bind(':ename', 'value4');
$database->execute();
echo $database->lastInsertId();
?>


Error's:
Well, when i try to execute (index.php) i have:


Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Nieznany host. in C:\WebserverXAMP\htdocs\php\PHP OOP\OOP Database\class\database.class.php on line 22

Fatal error: Call to a member function prepare() on a non-object in C:\WebserverXAMP\htdocs\php\PHP OOP\OOP Database\class\database.class.php on line 31


Anyone can help me with that ?
9:12 pm on Feb 16, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


The script can't connect to your database. Either the credentials you are passing are wrong, the database doesn't exist, or something else...
9:18 pm on Feb 16, 2014 (gmt 0)

Junior Member

joined:Nov 5, 2011
posts: 55
votes: 0


Yes i know :)
Jus waiting tillsomeone give a small example of code to work it out.

And answer is:

private $host = 'DB_HOST';
private $user = 'DB_USER';
private $pass = 'DB_PASS';
private $dbname = 'DB_NAME';


change to:


private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;


sort it out ..
10:15 pm on Feb 16, 2014 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Ah yes, that could well have been a problem! ;) Thanks for the follow up.