文件:db.config.smarty.php

这个文件主要是用于数据库配置

 <?php
$db = array(
'host'=>'localhost',
'user'=>'root',
'password'=>'********',
'database'=>'test',
)
?>

文件:db.conn.smarty.php

这个文件用于数据库操作,查询,删除,更新,添加。

要引用配置文件:require_once('db.config.smarty.php');

 <?php
class db {
public $conn;
public static $sql;
public static $instance=null;
private function __construct(){
require_once('db.config.smarty.php');
$this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
if(!mysql_select_db($db['database'],$this->conn)){
echo "失败";
};
mysql_query('set names utf8',$this->conn);
}
public static function getInstance(){
if(is_null(self::$instance)){
self::$instance = new db;
}
return self::$instance;
}
/**
* 查询数据库
*/
public function select($table,$condition=array(),$field = array()){
$where='';
if(!empty($condition)){ foreach($condition as $k=>$v){
$where.=$k."='".$v."' and ";
}
$where='where '.$where .'1=1';
}
$fieldstr = '';
if(!empty($field)){ foreach($field as $k=>$v){
$fieldstr.= $v.',';
}
$fieldstr = rtrim($fieldstr,',');
}else{
$fieldstr = '*';
}
self::$sql = "select {$fieldstr} from {$table} {$where}";
$result=mysql_query(self::$sql,$this->conn);
$resuleRow = array();
$i = 0;
while($row=mysql_fetch_assoc($result)){
foreach($row as $k=>$v){
$resuleRow[$i][$k] = $v;
}
$i++;
}
return $resuleRow;
}
/**
* 添加一条记录
*/
public function insert($table,$data){
$values = '';
$datas = '';
foreach($data as $k=>$v){
$values.=$k.',';
$datas.="'$v'".',';
}
$values = rtrim($values,',');
$datas = rtrim($datas,',');
self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})";
if(mysql_query(self::$sql)){
return mysql_insert_id();
}else{
return false;
};
}
/**
* 修改一记录
*/
public function update($table,$data,$condition=array()){
$where='';
if(!empty($condition)){ foreach($condition as $k=>$v){
$where.=$k."='".$v."' and ";
}
$where='where '.$where .'1=1';
}
$updatastr = '';
if(!empty($data)){
foreach($data as $k=>$v){
$updatastr.= $k."='".$v."',";
}
$updatastr = 'set '.rtrim($updatastr,',');
}
self::$sql = "update {$table} {$updatastr} {$where}";
return mysql_query(self::$sql);
}
/**
* 删除记录
*/
public function delete($table,$condition){
$where='';
if(!empty($condition)){ foreach($condition as $k=>$v){
$where.=$k."='".$v."' and ";
}
$where='where '.$where .'1=1';
}
self::$sql = "delete from {$table} {$where}";
return mysql_query(self::$sql); } public static function getLastSql(){
echo self::$sql;
}
}
?>

其他文件要操作数据的时候只要引用 require_once 'db.conn.smarty.php';就可以了。

 //require_once 'db.conn.smarty.php';
0 //$db = db::getInstance();
1 //$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding');
//$arr = $db->select('demo',array('password'=>'yujianqi2011'),array('username','age'));
//echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123'));
//echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
5 //echo $db->delete('demo',array('id'=>'2'));
//db::getLastSql();
//echo $list;

最新文章

  1. activiti 里面各个方法理解
  2. Android studio Gradle 教程
  3. 简述linux的发行版,并描述不同发行版之间的联系与区别
  4. TcpListener的异步调用内存泄漏---最近测试结果,没有泄露
  5. vim 插件管理
  6. ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
  7. js访sleep方法
  8. C# 中的 == 和 equals()有什么区别?
  9. 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
  10. HDU 4565 So Easy!(矩阵)
  11. Android开发之使用广播监听网络状态变化
  12. Spark处理Json格式数据(Python)
  13. web.xml中contextConfigLocation的作用(转)
  14. Java知识补充
  15. Bash命令行编辑
  16. LeetCode之“链表”:Intersection of Two Linked Lists
  17. window.open打开新窗体并用post方式传参
  18. Java8 Lambda和Stream的用法
  19. java中求余%与取模floorMod的区别
  20. sql server 索引碎片相关问题

热门文章

  1. django test模块
  2. 详解MathType中如何更改公式颜色
  3. Android开发学习笔记-自定义组合控件
  4. MLE、MAP、贝叶斯三种估计框架
  5. symfony 命令
  6. Fade out transition effect using CSS3
  7. Java 流(Stream)、文件(File)和IO -- Java ByteArrayInputStream类
  8. flexbox子盒子align-self属性
  9. 从Gallery中获取图片示例
  10. Java最快的maven仓库地址,国内Maven地址,超快的Maven地址