数据库操作相关(sql语句-php)

文件:db.config.smarty.php

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

1 <?php
2 $db = array(
3         'host'=>'localhost',
4         'user'=>'root',
5         'password'=>'********',
6         'database'=>'test',
7 )
8 ?>

文件:db.conn.smarty.php

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

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

  1 <?php
  2     class db {
  3         public $conn;
  4         public static $sql;
  5         public static $instance=null;
  6         private function __construct(){
  7             require_once('db.config.smarty.php');
  8             $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
  9             if(!mysql_select_db($db['database'],$this->conn)){
 10                 echo "失败";
 11             };
 12             mysql_query('set names utf8',$this->conn);        
 13         }
 14         public static function getInstance(){
 15             if(is_null(self::$instance)){
 16                 self::$instance = new db;
 17             }
 18             return self::$instance;
 19         }
 20         /**
 21          * 查询数据库
 22          */
 23         public function select($table,$condition=array(),$field = array()){
 24             $where='';
 25             if(!empty($condition)){
 26                 
 27                 foreach($condition as $k=>$v){
 28                     $where.=$k."='".$v."' and ";
 29                 }
 30                 $where='where '.$where .'1=1';
 31             }
 32             $fieldstr = '';
 33             if(!empty($field)){
 34                 
 35                 foreach($field as $k=>$v){
 36                     $fieldstr.= $v.',';
 37                 }
 38                  $fieldstr = rtrim($fieldstr,',');
 39             }else{
 40                 $fieldstr = '*';
 41             }
 42             self::$sql = "select {$fieldstr} from {$table} {$where}";
 43             $result=mysql_query(self::$sql,$this->conn);
 44             $resuleRow = array();
 45             $i = 0;
 46             while($row=mysql_fetch_assoc($result)){
 47                 foreach($row as $k=>$v){
 48                     $resuleRow[$i][$k] = $v;
 49                 }
 50                 $i++;
 51             }
 52             return $resuleRow;
 53         }
 54         /**
 55          * 添加一条记录
 56          */
 57          public function insert($table,$data){
 58              $values = '';
 59              $datas = '';
 60              foreach($data as $k=>$v){
 61                  $values.=$k.',';
 62                  $datas.="'$v'".',';
 63              }
 64              $values = rtrim($values,',');
 65              $datas   = rtrim($datas,',');
 66              self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
 67             if(mysql_query(self::$sql)){
 68                 return mysql_insert_id();
 69             }else{
 70                 return false;
 71             };
 72          }
 73          /**
 74           * 修改一记录
 75           */
 76         public function update($table,$data,$condition=array()){
 77             $where='';
 78             if(!empty($condition)){
 79                 
 80                 foreach($condition as $k=>$v){
 81                     $where.=$k."='".$v."' and ";
 82                 }
 83                 $where='where '.$where .'1=1';
 84             }
 85             $updatastr = '';
 86             if(!empty($data)){
 87                 foreach($data as $k=>$v){
 88                     $updatastr.= $k."='".$v."',";
 89                 }
 90                 $updatastr = 'set '.rtrim($updatastr,',');
 91             }
 92             self::$sql = "update {$table} {$updatastr} {$where}";
 93             return mysql_query(self::$sql);
 94         }
 95         /**
 96          * 删除记录
 97          */
 98          public function delete($table,$condition){
 99              $where='';
100             if(!empty($condition)){
101                 
102                 foreach($condition as $k=>$v){
103                     $where.=$k."='".$v."' and ";
104                 }
105                 $where='where '.$where .'1=1';
106             }
107             self::$sql = "delete from {$table} {$where}";
108             return mysql_query(self::$sql);
109             
110          }
111         
112         public static function getLastSql(){
113             echo self::$sql;
114         }         
115     }
116    ?>

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

0 //require_once 'db.conn.smarty.php';
0 //$db = db::getInstance();
1 //$arr = array(9 => 'Tennis', 3 => 'Swimming', 8 => 'Coding'); 2 //$arr = $db->select('demo',array('password'=>'yujianqi2011'),array('username','age')); 3 //echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123')); 4 //echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1)); 5 //echo $db->delete('demo',array('id'=>'2')); 6 //db::getLastSql(); 7 //echo $list;
学透前端行业所有技术,玩遍北京周边所有城市。然后我会回到那个生我养我的地方,因为有亲人的地方才是家。
原文地址:https://www.cnblogs.com/snowinmay/p/3167284.html