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