程序代碼方便大家使用
//MVC 實(shí)例 對(duì)于mysql的操作
<?php? //mysql.class.php
class Mysql
{
? ? /**報(bào)錯(cuò)函數(shù)
? ? ?* @param string $error
? ? **/
? ? ?
? ? function err($error)
? ? {
? ? ? ? die("對(duì)不起,您的操作有誤,錯(cuò)誤原因?yàn)椋?".$error);
? ? ? ? //die有兩種作用:輸出和終止,相當(dāng)于echo和exit的組合
? ? }
? ? ?
? ? ??
? ? ?//連接數(shù)據(jù)庫(kù)
? ? ?//@param string $config 匹配數(shù)組array($dbhost,$dbuser,$dbpwd,$dbname,$dbcharset)
? ? ?//@return bool 連接成功或失敗
? ? ?function connect($config)
? ? ?{
? ? ? ? ?extract($config);
? ? ? ? ?if(!($con=mysql_connect($dbhost,$dbuser,$dbpwd)))
? ? ? ? ?//mysql_connect連接數(shù)據(jù)庫(kù)函數(shù)
? ? ? ? ?$this->err(mysql_error());
? ? ? ? ?
? ? ? ? ?if(!mysql_select_db($dbname,$con))
? ? ? ? ?{
? ? ? ? ? ? ?$this->err(mysql_error());
? ? ? ? ?}
? ? ? ? ?
? ? ? ? ?mysql_query("set names ".$dbcharset);//使用mysql_query設(shè)置數(shù)據(jù)庫(kù)編碼/mysql_query(set names c utf8);
? ? ?}
? ? ??
? ? ? //執(zhí)行mysql query
? ? ? //@param string $sql
? ? ? //@return bool 返回執(zhí)行成功,資源或執(zhí)行失敗
? ? ? function query($sql)
? ? ? {
? ? ? ? ? if(!($query=mysql_query($sql)))
? ? ? ? ? //使用mysql_query來(lái)執(zhí)行sql語(yǔ)句
? ? ? ? ? {
? ? ? ? ? ? ? return $this->err($sql."<br />".mysql_error());
? ? ? ? ? }
? ? ? ? ? else
? ? ? ? ? {
? ? ? ? ? ? ? return $query;
? ? ? ? ? }
? ? ? }
? ? ??
? ? ? //列表
? ? ? //@param source $query? /sql通過(guò)mysql_query()執(zhí)行出來(lái)的資源
? ? ? //return array 返回列表數(shù)組
? ? ? function findAll($query)
? ? ? {
? ? ? ? ? while($rs=mysql_fetch_array($query, MYSQL_ASSOC))
? ? ? ? ??
? ? ? ? ? //MYSQL_ASSOC()作用將mysql_fetch_array()獲得的數(shù)組一次轉(zhuǎn)換出一行出來(lái)
? ? ? ? ? {
? ? ? ? ? ? ? $list[]=$rs;
? ? ? ? ? }
? ? ? ? ? return isset($list)?$list:"";
? ? ? }
? ? ??
? ? ? //返回單行的數(shù)據(jù)查詢(xún)結(jié)果
? ? ? function findOne($query)
? ? ? {
? ? ? ? ? $rs=mysql_fetch_array($query, MYSQL_ASSOC);
? ? ? ? ? return $rs;
? ? ? }
? ? ??
? ? ??
? ? ? //數(shù)據(jù)查詢(xún)返回指定行的指定字段的值
? ? ? //@param source $query? sql語(yǔ)句通過(guò)MySQL_query返回的資源
? ? ? //return array 返回指定行的指定字段的值
? ? ? function findResult($query,$row=0, $field=0)
? ? ? {
? ? ? ? ? $rs=mysql_result($query,$row,$field);
? ? ? ? ? return $rs;
? ? ? }
? ? ??
? ? ? //添加函數(shù) insert
? ? ? //@param string $table 表名
? ? ? //@param array $arr 添加數(shù)組(包含字段和值的數(shù)組)
? ? ? function insert($table, $arr)
? ? ? {
? ? ? ? ? //sql語(yǔ)句解釋 INSERT INTO table(表明:多個(gè)字段) VALUES(多個(gè)值)
? ? ? ? ? foreach($arr as $key=>$valule)
? ? ? ? ? //foreach 循環(huán)獲取數(shù)組
? ? ? ? ? {
? ? ? ? ? ? ? $value = mysql_real_escape_string($value);
? ? ? ? ? ? ? $keyArr[]="`".$key."`";//把$arr數(shù)組中的鍵名保存到$KeyArr數(shù)組中
? ? ? ? ? ? ? $valueArr[]="`".$value."`";//把$arr數(shù)組當(dāng)中的鍵值保存到$valueArr數(shù)組,
? ? ? ? ? ? ? //因?yàn)橹刀酁樽址鴖ql語(yǔ)句里面insert當(dāng)中如果值是字符串的話要加引號(hào),
? ? ? ? ? ? ? //所以這個(gè)地方要加上單引號(hào)(為esc鍵下方的單引號(hào))
? ? ? ? ? }
? ? ? ? ??
? ? ? ? ? $keys=implode(",",$keyArr);//implode函數(shù)把數(shù)組用(,)的組合成字符串
? ? ? ? ? $values=implode(",",$valueArr);//IMpolo的函數(shù)包值組合成字符串
? ? ? ? ? //然后進(jìn)行拼接sql語(yǔ)句
? ? ? ? ? $sql="INSERT INTO ".$table."(".$keys.") VALUES(".$values.");";
? ? ? ? ? $this->query($sql);
? ? ? ? ? return mysql_insert_id();
? ? ? }
? ? ??
? ? ? //修改函數(shù) update ssql語(yǔ)句
? ? ? //@param string $table 表名
? ? ? //@param array $arr? 修改的數(shù)組 字符段=字段值的一維數(shù)組
? ? ? //@parama string $where 修改的條件
? ? ? function update($table,$arr,$where)
? ? ? {
? ? ? ? foreach($arr as $key=>$value)
? ? ? ? {
? ? ? ? ? ? $vaule=mysql_real_escape_string($value);
? ? ? ? ? ? $keyAndValueArr[]="`".$key."`=`".$value."`";
? ? ? ? }
? ? ? ? $keyAndValues=imploed(",",$keyAndValuArr);
? ? ? ? $sql="UPDATE ".$table." SET ".$keyAndValues." WHERE ".$where;
? ? ? ? $this->query($sql);
? ? ? }
? ? ??
? ? ? //刪除函數(shù) delete sql語(yǔ)句
? ? ? //@param string $table 表名
? ? ? //@param string $where 條件
? ? ? function delete($table, $where)
? ? ? {
? ? ? ? ? $sql="DELETE FROM ".$table." WHERE ".$where;//sql刪除語(yǔ)句
? ? ? ? ? $this->query($sql);
? ? ? }
? ? ??
? ? ??
? ? ??
? ? ??
? ? ??
? ? ??
? ? ??
? ? ??
}
?>
2018-03-12
ok,ok