why it not working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hengwebdevelop
Forum Newbie
Posts: 1
Joined: Wed Feb 29, 2012 9:02 am

why it not working?

Post by hengwebdevelop »

I just want to create testing page by using pdo by separated class connection, Select, Insert, update, delete difference classes here my select class. Why it getting error it a few day ago i have no solution or i did it wrong or my logic it bad? can any body expert with php give me some idea about it pls.

Code: Select all

<?php
class PDO_DBConnect {
static $db ;
private $dbh ;
private function PDO_DBConnect () {
$db_type = 'mysql'; 
$db_name = 'testdb';
$user = 'root' ;
$password = '' ;
$host = 'localhost' ;
try {
$dsn = "$db_type:host=$host;dbname=$db_name";
$this->dbh = new PDO ( $dsn, $user, $password);
$this->dbh->setAttribute(PDO::ATTR_PERSISTENT, true);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch ( PDOException $e ) {
print "Error!: " . $e->getMessage () . "\n" ;
die () ;
}
}

public static function getInstance ( ) {
if (! isset ( PDO_DBConnect::$db )) {
PDO_DBConnect::$db = new PDO_DBConnect ( ) ;
}
return PDO_DBConnect::$db->dbh;
}
}
class ClassSelect
{
public function Select($field,$table)
{
$con = PDO_DBConnect::getInstance();
$sql = "SELECT ".$field." FROM ".$table;
$pre = $con->prepare($sql);
$pre->execute();
$pre->closeCursor();
$row = $pre->fetch(PDO::FETCH_ASSOC);
return $row;
}
}
$a= new ClassSelect();
$a->Select("field1","tbltest");
$row = $a->fetch(PDO::FETCH_ASSOC);
echo $row['field1'];
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: why it not working?

Post by Christopher »

Moved to PHP Code and reformatted post.
(#10850)
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: why it not working?

Post by temidayo »

What error are you getting?
Post Reply