ECshop--数据库模块细究

来源:互联网 发布:弹簧制作编程 编辑:程序博客网 时间:2024/06/05 09:22
早上交任务的时候又被老大批了一顿
$fieldsValue = array("id"=>"98","type"=>"uuu","id"=>"66","type"=>"iii");
话说能写出这样代码的人也真是人才,回头想想确实啊,想想自己都觉得好笑,
,哎,反正脸皮够厚,无所谓,能多学一点是一点,实习生就是各种挨锅。

总结下昨天的任务:
昨天把整个ecshop后面的数据库模块整理了下,对应的文件在include/cls_mysql.php下,仿照ecshop的getAll、getOne、getCol、insert、update、delete方法,自己改写测试了几遍,下面是测试的几个方法

<?phpheader("Content-type:text/html;charset=utf-8");include './conn.php';include_once './config.php';class Mysql {private $host;private $user;private $pwd;private $dbName;private $charset;private $conn = null;private $queryCount = 0;private $queryTime = '';private $queryLog = array();private $serviceStart = 0;private $errorMessage = array();public function connect($h, $u, $p, $n) {//提示PHP版本//判定数据库连接$conn = mysql_connect($h, $u, $p);mysql_select_db($n, $conn);$serviceStart = time();$this -> conn = $conn;}function query($sql) {//echo $this->conn;if($this->conn === NULL) {//连接$this->connect($host, $user, $pwd);echo "连接成功";}if($this->queryCount++ <= 99) {//查询次数自增,记录查询sql$this->queryLog[] = $sql;}if($this->queryTime == '') {//记录查询时间$this->queryTime = microtime(TRUE);}if($this->serviceStart +1 < time()) {mysql_ping();//重新连接}//var_dump($sql);//$queryValue = mysql_query($sql);if( !( $queryValue = mysql_query($sql) ) ) {//查询操作$this->errorMessage[] = mysql_error();//echo "1";return FALSE;}//var_dump($queryValue);return $queryValue;}/*public function deleteAssign($tableName,$index,$value) {$sql = "delete from ".$tableName." where ".$index." = ".$value;echo "<br>******************************<br>";echo $sql;echo "<br>******************************<br>";$result = mysql_query($sql);return mysql_affected_rows();}*/function selectLimit($sql, $num, $start = 0) {if($start == 0) {$sql .= " limit ".$num;}else {$sql .= " limit " . $start . "," . $num;}} public function delete($tableName) {$sql = "delete from ".$tableName;echo "<br>******************************<br>";echo $sql;echo "<br>******************************<br>";$result = mysql_query($sql);return mysql_affected_rows();}/*public function update($tableName,$updateIndex,$updateValue,$index,$value) {$where = "";if($index != ""&&$value != "")$where = " where ".$index." = '".$value."'";$sql = "update ".$tableName." set ".$updateIndex." = '".$updateValue."'".$where;echo "<br>******************************<br>";echo $sql;echo "<br>******************************<br>";$result = mysql_query($sql);return mysql_affected_rows();}*/public function getCol($sql){$result = $this->query($sql);//echo $result;if($result !== false) {$value = array();while($row = mysql_fetch_row($result)) {$value[] = $row[0];}//echo "yes";return $value;}else {//echo "no";return false;}}public function close() {mysql_close($this -> conn);}public function getOne($sql, $limited = false) {/*$sql = "select * from ".$tableName;$result = mysql_query($sql);echo "<br>******************************<br>";echo $sql;echo "<br>******************************<br>";return mysql_fetch_assoc($result);*/if($limited) {//限制取第一条记录$sql = trim($sql.' limit 1 ');}$result = $this->query($sql);if($result !== false) {$getIndex = mysql_fetch_row($result);//取得当前result首行记录,以index访问//var_dump($getIndex[2]);return $getIndex==true?$getIndex[0]:'';//0:取值第一个字段属性}else {//var_dump("123");return false;}}function getAll($sql) {//$tableName = "user";//$sql = "select * from ".$tableName;//$result = mysql_query($sql);/*echo "<br>******************************<br>";echo $sql;echo "<br>******************************<br>";*/$result = $this->query($sql);$values = array();if($result !== false) {while ($row = mysql_fetch_array($result)) { //循环获取所有值$values[] = $row; //获得当前行的值,然后添加到values数组中}return $values;}else {return false;}return $values;}//insert into ecs_nav (id,type)//value (99,'123')function insert($table, $fieldsValue) {$allFieldName = $this->getCol("desc ".$table);//获取所有字段//var_dump($allFieldName);$sql = "";$fields = $values = array(); //设定两个数组,field数组存放要插入数据的字段属性                             //values数组存放插入数据字段对应的值foreach ($allFieldName as $fieldName) {if(array_key_exists($fieldName, $fieldsValue) == true) { //查找传入的value数组,通过键值和字段name数组匹配 $fields[] = $fieldName;         //属性字段注入临时数组$values[] = "'" . $fieldsValue[$fieldName] . "'"; //属性值注入}}var_dump($values);if(empty($fields) == false) {       //判定临时存放字段数组非空$sql = "insert into " . $table . "(" . implode(', ', $fields) .  //implode拼装fields字符串") values (" . implode(", ", $values) . ")";}echo $sql;if($sql)return $this->query($sql)?"<br>insert OK<br>":"<br>insert error<br>";}function update($table, $fieldValues, $where = "") {  //where更新条件$allFieldName = $this->getCol("desc ".$table);$sql = "";$sets = array();foreach ($allFieldName as $fieldName) {if(array_key_exists($fieldName, $fieldValues) == true) { //拼装set语句$sets[] = $fieldName . " = '" . $fieldValues[$fieldName] . "'";}if(empty($sets) == false) {$sql = "update " . $table . " set " . implode(", ", $sets) . " where " . $where;}if($sql) return $this->query($sql)?"<br>update OK<br>":"<br>update error<br>";}}}?>

之前是看网上给的一些,写了一堆,然后被老大喷了一顿,囧

测试文件打包

http://download.csdn.net/detail/k183000860/9570069

0 0