批改狀態(tài):未批改
老師批語(yǔ):
<?php //連接數(shù)據(jù)庫(kù) if(!function_exists('connect')){ /** * [mysql數(shù)據(jù)庫(kù)連接] * @param [type] $dbname [description] * @param string $type [description] * @param string $host [description] * @param string $charset [description] * @param integer $port [description] * @param string $user [description] * @param string $pass [description] * @return [type] [description] */ function connect($dbname,$type='mysql',$host='127.0.0.1',$charset='utf8',$port=3306,$user='root',$pass='root') { $dsn="{$type}:host=[$host];dbname={$dbname};charset={$charset};port={$port}"; $username=$user; $password=$pass; $options=[ PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,//設(shè)置錯(cuò)誤模式 PDO::ATTR_CASE=>PDO::CASE_NATURAL,//數(shù)據(jù)表字段保持不變,自然名稱 PDO::ATTR_EMULATE_PREPARES=>true,//啟用PDO模擬 PDO::ATTR_PERSISTENT=>true,//啟用持久性連接 ]; try{ //實(shí)例化PDO類,創(chuàng)建PDO對(duì)象 $pdo = new PDO($dsn, $username, $password, $options);echo '數(shù)據(jù)庫(kù)連接成功(請(qǐng)刪除)<br>'; } catch (PDOException $e) { die('Connect ERROR!:'.$e->getMessage()); } return $pdo; }} //插入數(shù)據(jù) if(!function_exists('insert')){ /** * 增加數(shù)據(jù) * @param [type] $pdo [description] * @param [type] $table [description] * @param array $data [description] * @return [type] [description] */ function insert($pdo,$table,$data=[]) { //創(chuàng)建sql 語(yǔ)句 $sql="INSERT IGNORE {$table} SET ";//數(shù)據(jù)庫(kù) 鍵值要設(shè)為唯一ignore 才有效果 //提取數(shù)據(jù)中的鍵,放入sql foreach (array_keys($data) as $field) { $sql .=$field.' =:'.$field.','; } //去除“,”號(hào),加上“;”號(hào) $sql=rtrim(trim($sql),',').';';//die($sql);//檢測(cè)sql語(yǔ)句 $yu=$pdo->prepare($sql); //綁定 foreach ($data as $key => $value) { $yu->bindValue(":{$key}",$value); } //執(zhí)行 if($yu->execute()){ if($yu->rowCount()>0){ return true; } }else{ return false; } }} //刪除數(shù)據(jù) if(!function_exists('delete')){ /** * [delete description] * @param [type] $pdo [description] * @param [type] $table [description] * @param string $where [description] * @return [type] [description] */ function delete($pdo,$table,$where=''){ $sql="DELETE FROM {$table} "; if (!empty($where)) { $sql .='WHERE '.$where.';'; }else{ exit('刪除數(shù)據(jù),條件不能為空'); } //die($sql); $yu=$pdo->prepare($sql); if($yu->execute()){ if($yu->rowCount()>0){ echo '刪除成功'; return true; } }else{ echo '刪除失敗'; return false; exit(); } }} //修改數(shù)據(jù) if (!function_exists('update')) { function update($pdo,$table,$data=[],$where=''){ $sql="UPDATE {$table} SET "; foreach (array_keys($data) as $key) { $sql.=$key.'=:'.$key.','; } $sql=rtrim(trim($sql),','); if (!empty($where)) { $sql .=' WHERE '.$where.';'; }else{ exit('修改數(shù)據(jù),條件不能為空'); } // die($sql); $yu=$pdo->prepare($sql); foreach ($data as $key => $value) { $yu->bindValue(":{$key}",$value); } //die($sql); if($yu->execute()){ if($yu->rowCount()>0){ echo '修改成功'; return true; } }else{ echo '修改失敗'; return false; }}} //單條語(yǔ)句查詢 if(!function_exists('one_select')){ /** * [one_select description] * @param [type] $pdo [description] * @param [type] $table [description] * @param [type] $fields [description] * @param string $where [description] * @return [type] [description] */ function one_select($pdo,$table,$fields,$where=''){ $sql="SELECT "; if(!empty($fields)){ if(is_array($fields)){ foreach ($fields as $key ) { $sql.=$key.','; } }else{ $sql.=$fields; } }else{ echo '沒(méi)有查詢字段'; exit(); } $sql=rtrim(trim($sql),','); $sql.=' FROM '.$table; if(!empty($where)) { $sql .= ' WHERE '. $where.';'; }else{ $sql .=' LIMIT 1;';//沒(méi)有條件,就顯示第一條記錄 } //die($sql); $yu=$pdo->prepare($sql); if($yu->execute()){ if($yu->rowCount()>0){ $yu->setFetchMode(PDO::FETCH_ASSOC); return $yu->fetch(); } }else{ return false; } } } //多條語(yǔ)句查詢 if(!function_exists('duo_select')){ /** * [duo_select description] * @param [type] $pdo [description] * @param [type] $table [description] * @param [type] $fields [description] * @param string $where [description] * @param string $order [description] * @return [type] [description] */ function duo_select($pdo,$table,$fields,$where='',$order=''){ $sql='SELECT '; if(is_array($fields)){ foreach($fields as $key){ $sql.=$key.','; } }else{ $sql.=$fields; } $sql=rtrim(trim($sql),','); $sql.=' FROM '.$table; if(!empty($where)){ $sql.=' WHERE '.$where; } if(!empty($order)){ $sql.=' order by '.$order; } $sql = rtrim(trim($sql),',').';'; //die($sql); //創(chuàng)建PDO預(yù)處理對(duì)象 $stmt = $pdo->prepare($sql); //die($yu->queryString);//查看 //執(zhí)行查詢操作 if($stmt->execute()){ if($stmt->rowCount()>0){ $stmt->setFetchMode(PDO::FETCH_ASSOC); //返回一個(gè)二維數(shù)組 return $stmt->fetchAll(); } } else { return false; } } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)