homepage Welcome to WebmasterWorld Guest from 54.197.65.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PDO database query problem
Call to a member function prepare() on a non-object
bethesda



 
Msg#: 4645709 posted 12:32 pm on Feb 16, 2014 (gmt 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 ?

 

penders

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



 
Msg#: 4645709 posted 9:12 pm on Feb 16, 2014 (gmt 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...

bethesda



 
Msg#: 4645709 posted 9:18 pm on Feb 16, 2014 (gmt 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 ..

penders

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



 
Msg#: 4645709 posted 10:15 pm on Feb 16, 2014 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved