why it not working?
Posted: Wed Feb 29, 2012 9:11 am
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'];