westdc-zf1/application/models/helper/dbh.php

50 lines
946 B
PHP
Raw Normal View History

<?php
class dbh
{
private $db; //传入PDO对象.
function __construct($db)
{
$this->db = $db;
}
function insert($table,$data,$return=false)
{
$fields = array();
$datas = array();
foreach($data as $k=>$v)
{
$fields[] = '"'.$k.'"';
if(is_int($v) || is_float($v) || is_bool($v))
{
$datas[] = $v;
}else{
$datas[] = "'".$v."'";
}
}
if($return == false){
$fields = join(",",$fields);
$datas = join(",",$datas);
$sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas)";
return $this->db->exec($sql);
}else{
$sql = "INSERT INTO \"".$table."\" ($fields) VALUES ($datas) RETURNING id";
try{
$sth = $this->db->prepare($sql);
if($sth->execute())
{
$temp = $sth->fetch(PDO::FETCH_ASSOC);
return $temp['id'];
}else{
return false;
}
}catch(Exception $e)
{
return false;
}
}
}
}