1. 云栖社区>
  2. PHP教程>
  3. 正文

基于php和mysql的简单的dao类

作者:用户 来源:互联网 时间:2017-12-01 16:51:50

mysqlphp简单dao基于

基于php和mysql的简单的dao类 - 摘要: 本文讲的是基于php和mysql的简单的dao类, 一个简单的dao,实现基本的CRUD功能,可以继承扩展为实际业务的dao类,当然也可以直接使用<?php//require_once('FirePHPCore/FirePHP.class.php'); //$firephp = Fi


一个简单的dao,实现基本的CRUD功能,可以继承扩展为实际业务的dao类,当然也可以直接使用

<?php
//require_once('FirePHPCore/FirePHP.class.php');
//$firephp = FirePHP::getInstance(true); // debugger in firefox
class SimpleDao {
private $_table = null;
private static $_con = null;
public function SimpleDao() {
if ($this->_con == null) {
$this->_con = @mysql_connect("localhost","root","123456");
if ($this->_con == FALSE) {
echo("connect to db server failed.");
$this->_con = null;
return;
}
//$firephp->log("new DAO object");
@mysql_select_db("swan", $this->_con);
}
}
public function table($tablename) {
$this->_table = $tablename;
return $this;
}
public function query($sql) {
$result = @mysql_query($sql);
$ret = [];
if ($result) {
while ($row = mysql_fetch_array($result)) {
$ret[] = $row;
}
}
return $ret;
}
public function get($where = null) {
$sql ="select * from".$this->_table;
//$sql = $sql.$this->_getWhereString($where);
//echo"[get]".$sql."
";
return $this->query($sql);
}
public function insert($params) {
if ($params == null || !is_array($params)) {
return -1;
}
$keys = $this->_getParamKeyString($params);
$vals = $this->_getParamValString($params);
$sql ="insert into".$this->_table."(".$keys.") values(".$vals.")";
//echo"[insert]".$sql."
";
$result = @mysql_query($sql);
if (! $result) {
return -1;
}
return @mysql_insert_id();
}
public function update($params, $where = null) {
if ($params == null || !is_array($params)) {
return -1;
}
$upvals = $this->_getUpdateString($params);
$wheres = $this->_getWhereString($where);
$sql ="update".$this->_table."set".$upvals."".$wheres;
//echo"[update]".$sql."
";
$result = @mysql_query($sql);
if (! $result) {
return -1;
}
return @mysql_affected_rows();
}
public function delete($where) {
$wheres = $this->_getWhereString($where);
$sql ="delete from".$this->_table.$wheres;
//echo"[delete]".$sql."
";
$result = @mysql_query($sql);
if (! $result) {
return -1;
}
return @mysql_affected_rows();
}
protected function _getParamKeyString($params) {
$keys = array_keys($params);
return implode(",", $keys);
}
protected function _getParamValString($params) {
$vals = array_values($params);
return"'".implode("','", $vals)."'";
}
private function _getUpdateString($params) {
//echo"_getUpdateString";
$sql ="";
if (is_array($params)) {
$sql = $this->_getKeyValString($params,",");
}
return $sql;
}
private function _getWhereString($params) {
//echo"_getWhereString";
$sql ="";
if (is_array($params)) {
$sql ="where";
$where = $this->_getKeyValString($params,"and");
$sql = $sql.$where;
}
return $sql;
}
private function _getKeyValString($params, $split) {
$str ="";
if (is_array($params)) {
$paramArr = array();
foreach($params as $key=>$val) {
$valstr = $val;
if (is_string($val)) {
$valstr ="'".$val."'";
}
$paramArr[] = $key."=".$valstr;
}
$str = $str.implode($split, $paramArr);
}
return $str;
}
public function release() {
@mysql_close();
}
}
function T($table) {
return (new SimpleDao())->table($table);
}
?>
<?php
include"test/simpledao.php";
$dao = T("sw_post");
$result = $dao->get();//get all posts
$dao->release();
echo json_encode($result);
?>
<?php
include"test/simpledao.php";
$dao = T("sw_post");
// update title where id=1
$cnt = $dao->update(array("title"=>"Hello REST2"), array("id"=>1));
$dao->release();
echo json_encode(array("count"=>$cnt));
?>
<?php
include"test/simpledao.php";
$dao = T("sw_tag");
// insert new record
$cnt = $dao->insert(array("postid"=>4,"name"=>"测试TAG"));
$dao->release();
echo json_encode(array("count"=>$cnt));
?>
<?php
include"test/simpledao.php";
$dao = T("sw_tag");
// delete from table where name='测试TAG'
$cnt = $dao->delete(array("name"=>"测试TAG"));
$dao->release();
echo json_encode(array("count"=>$cnt));
?>

以上是基于php和mysql的简单的dao类的全部内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有其他相关内容,欢迎继续使用右上角搜索按钮进行搜索mysql , php , 简单 , dao 基于 ,以便于您获取更多的相关知识。