Sie sind auf Seite 1von 2

<?

php
error_reporting(0);
function Connection(){
$obj= new DBObject("localhost","database","root","1234");
$obj->execute("set names utf8",array());
return($obj);
}
class DBObject {
private
private
private
private

$conn;
$rs;
$mode;
$debug=false;

function __construct($host,$database,$usuario,$password=""){
try{
$this->conn = new PDO("mysql:host={$host};dbname={$database}", $usua
rio, $password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
;
$this->fetchObject();
} catch(PDOException $e){
echo "ERROR: " . $e->getMessage();
}
}
function debug($d=true){
$this->debug=$d;
}
function showDebug($sql,$e){
switch($e->getCode()){
case 23000:
$_SESSION["msg"]="Lo sentimos la informacin est duplicada o no existe.
";
break;
default:
$_SESSION["msg"]= "";
}
if($this->debug) echo "<p>$sql<br><font color=red>" . $e->getMessage() .
"</font><p>";
}
function fetchObject(){
$this->mode = PDO::FETCH_OBJ;
}
function fetchArray(){
$this->mode = PDO::FETCH_BOTH;
}
function query($sql,$params,$mode="OBJECT")
{
//if($mode=="ARRAY") $this->fetchObject(); else $this->fetchObject();
try {
$this->rs = $this->conn->prepare($sql);
$this->rs->execute($params);
$this->rs->setFetchMode($this->mode);
return($this->rs);
}

catch(Exception $e) {
$this->showDebug($sql,$e);
}
}
function count() {
return($this->rs->rowCount());
}
function record($sql,$params)
{
$this->fetchObject();
$this->query($sql,$params);
return ($this->next());
}
function recordArray($sql,$params)
{
$this->fetchArray();
$this->query($sql,$params);
return ($this->next());
}
function field($sql,$params)
{
$row = $this->recordArray($sql,$params);
return ($row[0]);
}
function execute($sql,$params)
{
try {
$this->rs = $this->conn->prepare($sql);
$this->rs->execute($params);
}
catch(Exception $e) {
$this->showDebug($sql,$e);
}
}
function next()
{
return($this->rs->fetch());
}
}
?>

Das könnte Ihnen auch gefallen