亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

程序代碼方便大家使用

//MVC 實例 對于mysql的操作


<?php? //mysql.class.php

class Mysql

{

? ? /**報錯函數

? ? ?* @param string $error

? ? **/

? ? ?

? ? function err($error)

? ? {

? ? ? ? die("對不起,您的操作有誤,錯誤原因為: ".$error);

? ? ? ? //die有兩種作用:輸出和終止,相當于echo和exit的組合

? ? }

? ? ?

? ? ??

? ? ?//連接數據庫

? ? ?//@param string $config 匹配數組array($dbhost,$dbuser,$dbpwd,$dbname,$dbcharset)

? ? ?//@return bool 連接成功或失敗

? ? ?function connect($config)

? ? ?{

? ? ? ? ?extract($config);

? ? ? ? ?if(!($con=mysql_connect($dbhost,$dbuser,$dbpwd)))

? ? ? ? ?//mysql_connect連接數據庫函數

? ? ? ? ?$this->err(mysql_error());

? ? ? ? ?

? ? ? ? ?if(!mysql_select_db($dbname,$con))

? ? ? ? ?{

? ? ? ? ? ? ?$this->err(mysql_error());

? ? ? ? ?}

? ? ? ? ?

? ? ? ? ?mysql_query("set names ".$dbcharset);//使用mysql_query設置數據庫編碼/mysql_query(set names c utf8);

? ? ?}

? ? ??

? ? ? //執行mysql query

? ? ? //@param string $sql

? ? ? //@return bool 返回執行成功,資源或執行失敗

? ? ? function query($sql)

? ? ? {

? ? ? ? ? if(!($query=mysql_query($sql)))

? ? ? ? ? //使用mysql_query來執行sql語句

? ? ? ? ? {

? ? ? ? ? ? ? return $this->err($sql."<br />".mysql_error());

? ? ? ? ? }

? ? ? ? ? else

? ? ? ? ? {

? ? ? ? ? ? ? return $query;

? ? ? ? ? }

? ? ? }

? ? ??

? ? ? //列表

? ? ? //@param source $query? /sql通過mysql_query()執行出來的資源

? ? ? //return array 返回列表數組

? ? ? function findAll($query)

? ? ? {

? ? ? ? ? while($rs=mysql_fetch_array($query, MYSQL_ASSOC))

? ? ? ? ??

? ? ? ? ? //MYSQL_ASSOC()作用將mysql_fetch_array()獲得的數組一次轉換出一行出來

? ? ? ? ? {

? ? ? ? ? ? ? $list[]=$rs;

? ? ? ? ? }

? ? ? ? ? return isset($list)?$list:"";

? ? ? }

? ? ??

? ? ? //返回單行的數據查詢結果

? ? ? function findOne($query)

? ? ? {

? ? ? ? ? $rs=mysql_fetch_array($query, MYSQL_ASSOC);

? ? ? ? ? return $rs;

? ? ? }

? ? ??

? ? ??

? ? ? //數據查詢返回指定行的指定字段的值

? ? ? //@param source $query? sql語句通過MySQL_query返回的資源

? ? ? //return array 返回指定行的指定字段的值

? ? ? function findResult($query,$row=0, $field=0)

? ? ? {

? ? ? ? ? $rs=mysql_result($query,$row,$field);

? ? ? ? ? return $rs;

? ? ? }

? ? ??

? ? ? //添加函數 insert

? ? ? //@param string $table 表名

? ? ? //@param array $arr 添加數組(包含字段和值的數組)

? ? ? function insert($table, $arr)

? ? ? {

? ? ? ? ? //sql語句解釋 INSERT INTO table(表明:多個字段) VALUES(多個值)

? ? ? ? ? foreach($arr as $key=>$valule)

? ? ? ? ? //foreach 循環獲取數組

? ? ? ? ? {

? ? ? ? ? ? ? $value = mysql_real_escape_string($value);

? ? ? ? ? ? ? $keyArr[]="`".$key."`";//把$arr數組中的鍵名保存到$KeyArr數組中

? ? ? ? ? ? ? $valueArr[]="`".$value."`";//把$arr數組當中的鍵值保存到$valueArr數組,

? ? ? ? ? ? ? //因為值多為字符串,而sql語句里面insert當中如果值是字符串的話要加引號,

? ? ? ? ? ? ? //所以這個地方要加上單引號(為esc鍵下方的單引號)

? ? ? ? ? }

? ? ? ? ??

? ? ? ? ? $keys=implode(",",$keyArr);//implode函數把數組用(,)的組合成字符串

? ? ? ? ? $values=implode(",",$valueArr);//IMpolo的函數包值組合成字符串

? ? ? ? ? //然后進行拼接sql語句

? ? ? ? ? $sql="INSERT INTO ".$table."(".$keys.") VALUES(".$values.");";

? ? ? ? ? $this->query($sql);

? ? ? ? ? return mysql_insert_id();

? ? ? }

? ? ??

? ? ? //修改函數 update ssql語句

? ? ? //@param string $table 表名

? ? ? //@param array $arr? 修改的數組 字符段=字段值的一維數組

? ? ? //@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);

? ? ? }

? ? ??

? ? ? //刪除函數 delete sql語句

? ? ? //@param string $table 表名

? ? ? //@param string $where 條件

? ? ? function delete($table, $where)

? ? ? {

? ? ? ? ? $sql="DELETE FROM ".$table." WHERE ".$where;//sql刪除語句

? ? ? ? ? $this->query($sql);

? ? ? }

? ? ??

? ? ??

? ? ??

? ? ??

? ? ??

? ? ??

? ? ??

? ? ??

}






















?>


正在回答

1 回答

ok,ok

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

程序代碼方便大家使用

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號