知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术 > PHP编程

php实战第二天

发布时间:2015-05-27 19:27:16作者:知识屋

/ 

1.我的留言板上写上了%20add方法

[php] %20function%20add(){    %20//添加后返回%20影响条数,如果大于0就说明添加成功     %20if($this->db->data($_POST)->add()>0){        %20echo%20"添加成功";        %20// %20echo%20"<script>location.reload()</script>";//防止刷新后的表单的重复提交         %20Header("HTTP/1.1%20303%20See%20Other");        %20Header("Location:%20");  %20//转向到根目录         %20exit;             %20}else%20{        %20die($this->db->error());//添加失败输出错误信息     %20}    

  function%20add(){   //添加后返回%20影响条数,如果大于0就说明添加成功   if($this->db->data($_POST)->add()>0){    echo%20"添加成功";    // echo%20"<script>location.reload()</script>";//防止刷新后的表单的重复提交    Header("HTTP/1.1%20303%20See%20Other");    Header("Location:%20");  %20//转向到根目录    exit;       }else%20{    die($this->db->error());//添加失败输出错误信息   }   }对应提交表单

[html] %20<div%20class="center">    %20<form%20action="?m=index&a=add"%20method="post">        %20<table%20border=0>                        %20<tr>                            %20<td>用户名</td>                            %20<td><input%20type="text"%20name="userName"%20value="admin"></td>                        %20</tr>                        %20<tr>                            %20<td>留言内容</td>                            %20<td><textarea%20rows="3"%20cols="20"%20name="content">aaaaa</textarea></td>                        %20</tr>                        %20<tr>                            %20<td>电子邮箱</td>                            %20<td>email:<input%20type="text"%20name="email"%20value="admin@admin.com"></td>                        %20</tr>                        %20<tr>                            %20<td></td>                            %20<td><input%20type="submit"%20name="submit"%20value="留言"></td>                        %20</tr>        %20</table>                                 %20<input%20type="hidden"%20name="action"%20value="add">                %20</form>            %20</div> </div> 

<div%20class="center"> <form%20action="?m=index&a=add"%20method="post">   <table%20border=0>                       %20<tr>                           %20<td>用户名</td>                           %20<td><input%20type="text"%20name="userName"%20value="admin"></td>                       %20</tr>                       %20<tr>                           %20<td>留言内容</td>                           %20<td><textarea%20rows="3"%20cols="20"%20name="content">aaaaa</textarea></td>                       %20</tr>                       %20<tr>                           %20<td>电子邮箱</td>                           %20<td>email:<input%20type="text"%20name="email"%20value="admin@admin.com"></td>                       %20</tr>                       %20<tr>                           %20<td></td>                           %20<td><input%20type="submit"%20name="submit"%20value="留言"></td>                       %20</tr>  </table>                              %20<input%20type="hidden"%20name="action"%20value="add">               %20</form>   </div></div>

2.修改了MYSQL操作类%20使%20data%20自动处理传递过来的数据,对比是否存在字段,不存在不添加.

[php] %20<?php //%20+----------------------------------------------------------------------  //%20|MySQL操作类  //%20+----------------------------------------------------------------------  //%20|@微凉%20QQ:496928838  //%20+----------------------------------------------------------------------  class%20MySQL{         %20private%20$db_mysql_hostname;    %20private%20$db_mysql_username;    %20private%20$db_mysql_password;    %20private%20$db_mysql_database;    %20private%20$db_mysql_port;    %20private%20$db_mysql_charset;         %20private%20$query_list%20=%20array();         %20//查询次数     %20public%20$query_count%20=%200;    %20//查询开始时间     %20public%20$query_start_time;         %20//当前查询ID     %20protected%20$queryID;    %20//当前连接     %20protected%20$conn;    %20//%20事务指令数     %20protected%20$transTimes%20=%200;    %20//%20返回或者影响记录数     %20protected%20$numRows   %20=%200;    %20//%20错误信息     %20protected%20$error     %20=%20'';         %20public%20function%20__construct($hostname_or_conf,$username,$password,$database,$port%20=%20'3306',$char%20=%20'utf8'){        %20if(is_array($hostname_or_conf)){            %20$this->db_mysql_hostname%20=%20$hostname_or_conf['hostname'];            %20$this->db_mysql_username%20=%20$hostname_or_conf['username'];            %20$this->db_mysql_password%20=%20$hostname_or_conf['password'];            %20$this->db_mysql_database%20=%20$hostname_or_conf['database'];            %20$this->db_mysql_port%20=%20isset($hostname_or_conf['port'])?$hostname_or_conf['port']:'3306';            %20$this->db_mysql_charset%20=%20isset($hostname_or_conf['charset'])?$hostname_or_conf['charset']:'utf8';                     %20}elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database))        %20{             %20$this->db_mysql_hostname%20=%20$hostname_or_conf;             %20$this->db_mysql_username%20=%20$username;             %20$this->db_mysql_password%20=%20$password;             %20$this->db_mysql_database%20=%20$database;             %20$this->db_mysql_port%20=%20$port;             %20$this->db_mysql_charset%20=%20$char;                      %20}else{            %20die('configuration%20error.');        %20}        %20$this->connect();    %20}         %20private%20function%20connect(){        %20$server%20=%20$this->db_mysql_hostname.':'.$this->db_mysql_port;        %20$this->conn%20=%20mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true)%20or%20die('Connect%20MySQL%20DB%20error!');        %20mysql_select_db($this->db_mysql_database,$this->conn)%20or%20die('select%20db%20error!');        %20mysql_query("set%20names%20"%20.%20$this->db_mysql_charset,%20$this->conn);    %20}    %20/**    %20+----------------------------------------------------------    %20*%20设置数据对象值    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*table,where,order,limit,data,field,join,group,having    %20+----------------------------------------------------------    %20*/    %20public%20function%20table($table){        %20$this->query_list['table']%20=%20$table;        %20return%20$this;    %20}         %20public%20function%20where($where){        %20$this->query_list['where']%20=%20$where;        %20return%20$this;    %20}         %20public%20function%20order($order){        %20$this->query_list['order']%20=%20$order;        %20return%20$this;    %20}         %20public%20function%20limit($offset,$length){        %20if(!isset($length)){            %20$length%20=%20$offset;            %20$offset%20=%200;        %20}        %20$this->query_list['limit']%20=%20'limit%20'.$offset.','.$length;        %20return%20$this;    %20}         %20public%20function%20data($data){        %20//读取数据表字段,然后处理表单数据         %20$dataList%20=%20$this->getFields($this->query_list['table']);        %20$arr=array();        %20foreach%20($dataList%20as%20$key=>$value)%20{            %20if%20(array_key_exists%20($key,$data)%20)%20{                %20$arr[$key]=$data[$key];            %20}                     %20}        %20//var_dump($arr);         %20/*       %20if(is_object($data)){           %20$data  %20=  %20get_object_vars($data);       %20}elseif%20(is_string($data)){           %20parse_str($data,$data);       %20}elseif(!is_array($data)){           %20//Log:DATA_TYPE_INVALID       %20}       %20*/        %20$this->query_list['data']%20=%20$arr;        %20return%20$this;    %20}    %20public%20function%20field($fields){        %20$this->query_list['fields']%20=%20$fields;        %20return%20$this;    %20}    %20public%20function%20join($join){        %20$this->query_list['join']%20=%20$join;        %20return%20$this;    %20}    %20public%20function%20group($group){        %20$this->query_list['group']%20=%20$group;        %20return%20$this;    %20}    %20public%20function%20having($having){        %20$this->query_list['having']%20=%20$having;        %20return%20$this;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20查询    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/    %20public%20function%20select(){        %20$select_sql%20=%20'select%20';        %20$fields%20=%20isset($this->query_list['fields'])?$this->query_list['fields']:'*';        %20$select_sql.=$fields;        %20$select_sql.=%20'%20from%20`'.$this->query_list['table'].'`%20';                 %20isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):'';        %20isset($this->query_list['where'])?($select_sql.='%20where%20'.$this->query_list['where']):'';        %20isset($this->query_list['group'])?($select_sql.='%20group%20by'.$this->query_list['group']):'';        %20isset($this->query_list['having'])?($select_sql.='%20mysql%20having%20'.$this->query_list['having']):'';        %20isset($this->query_list['order'])?($select_sql.='%20order%20by%20'.$this->query_list['order']):'';        %20isset($this->query_list['limit'])?($select_sql.='%20'.$this->query_list['limit']):'';                 %20return%20$this->query($select_sql);    %20}    %20/**    %20+----------------------------------------------------------    %20*%20增加    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/    %20public%20function%20add(){        %20$add_sql%20=%20'insert%20into%20`'.$this->query_list['table'].'`%20(';                 %20$data%20=%20$this->query_list['data'];        %20$value%20=%20$field%20=%20'';        %20foreach($data%20as%20$k=>$v){            %20$field%20.=%20'`'.$k.'`,';            %20if(is_numeric($v))                %20$value%20.=%20$v.',';            %20else                %20$value%20.=%20'/''.$v.'/',';        %20}        %20$add_sql%20.=%20rtrim($field,',').')%20values%20('.rtrim($value,',').')';     %20// %20echo%20'add_sql'.$add_sql;         %20return%20$this->execute($add_sql);    %20}    %20/**    %20+----------------------------------------------------------    %20*%20删除    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/    %20public%20function%20delete(){        %20$del_sql%20=%20'delete%20from%20`'.$this->query_list['table'].'`%20where%20'.$this->query_list['where'];                 %20if(isset($this->query_list['order']))            %20$del_sql%20.=%20'order%20by%20'.$this->query_list['order'];        %20if(isset($this->query_list['limit']))            %20$del_sql%20.=%20'%20'.$this->query_list['limit'];                     %20return%20$this->execute($del_sql);             %20}    %20/**    %20+----------------------------------------------------------    %20*%20更新    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/    %20public%20function%20update(){        %20$update_sql%20=%20'update%20`'.$this->query_list['table'].'`%20set%20';        %20$data%20=%20$this->query_list['data'];                 %20foreach($data%20as%20$k=>$v){            %20if(is_numeric($v))                %20$update_sql%20.=%20'`'.$k.'`%20='.$v.',';            %20else                %20$update_sql%20.=%20'`'.$k.'`%20=/''.$v.'/',';        %20}        %20$update_sql%20=%20rtrim($update_sql,',');        %20if(isset($this->query_list['where']))            %20$update_sql%20.=%20'%20where%20'.$this->query_list['where'];        %20if(isset($this->query_list['order']))            %20$update_sql%20.=%20'%20order%20by%20'.$this->query_list['order'];        %20if(isset($this->query_list['limit']))            %20$update_sql%20.=%20'%20'.$this->query_list['limit'];                 %20return%20$this->execute($update_sql);             %20}     %20/**    %20+----------------------------------------------------------    %20*%20执行查询%20返回数据集    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param%20string%20$sql %20sql指令    %20*/    %20public%20function%20query($sql)%20{        %20if%20(%20!$this->conn%20)%20return%20false;        %20$this->queryStr%20=%20$sql;        %20//释放前次的查询结果         %20if%20(%20$this->queryID%20)%20{   %20$this->free();   %20}                 %20$this->query_start_time%20=%20microtime(true);                 %20$this->queryID%20=%20mysql_query($sql,%20$this->conn);        %20$this->query_count++;        %20if%20(%20false%20===%20$this->queryID%20)%20{            %20$this->error();            %20return%20false;        %20}%20else%20{            %20$this->numRows%20=%20mysql_num_rows($this->queryID);            %20return%20$this->getAll();        %20}    %20}    %20/**    %20+----------------------------------------------------------    %20*%20执行语句    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param%20string%20$sql %20sql指令    %20+----------------------------------------------------------    %20*/    %20public%20function%20execute($sql)%20{        %20if%20(%20!$this->conn%20)%20return%20false;        %20$this->queryStr%20=%20$sql;        %20//释放前次的查询结果         %20if%20(%20$this->queryID%20)%20{   %20$this->free();   %20}                 %20$this->query_start_time%20=%20microtime(true);                 %20$result%20=  %20mysql_query($sql,%20$this->conn)%20;        %20$this->query_count++;        %20if%20(%20false%20===%20$result)%20{            %20$this->error();            %20return%20false;        %20}%20else%20{            %20$this->numRows%20=%20mysql_affected_rows($this->conn);            %20return%20$this->numRows;        %20}    %20}    %20/**    %20+----------------------------------------------------------    %20*%20获得所有的查询数据    %20+----------------------------------------------------------    %20*%20@access%20private    %20+----------------------------------------------------------    %20*%20@return%20array    %20*/    %20private%20function%20getAll()%20{        %20//返回数据集         %20$result%20=%20array();        %20if($this->numRows%20>0)%20{            %20while($row%20=%20mysql_fetch_assoc($this->queryID)){                %20$result[]  %20=  %20$row;            %20}            %20mysql_data_seek($this->queryID,0);        %20}        %20return%20$result;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20取得数据表的字段信息    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/    %20public%20function%20getFields($tableName)%20{        %20$result%20=  %20$this->query('SHOW%20COLUMNS%20FROM%20`'.$tableName.'`');        %20$info  %20=  %20array();        %20if($result)%20{            %20foreach%20($result%20as%20$key%20=>%20$val)%20{                %20$info[$val['Field']]%20=%20array(                    %20'name'   %20=>%20$val['Field'],                    %20'type'   %20=>%20$val['Type'],                    %20'notnull'%20=>%20(bool)%20($val['Null']%20===%20''),%20//%20not%20null%20is%20empty,%20null%20is%20yes                     %20'default'%20=>%20$val['Default'],                    %20'primary'%20=>%20(strtolower($val['Key'])%20==%20'pri'),                    %20'autoinc'%20=>%20(strtolower($val['Extra'])%20==%20'auto_increment'),                %20);            %20}        %20}        %20return%20$info;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20取得数据库的表信息    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/    %20public%20function%20getTables($dbName='')%20{        %20if(!empty($dbName))%20{           %20$sql   %20=%20'SHOW%20TABLES%20FROM%20'.$dbName;        %20}else{           %20$sql   %20=%20'SHOW%20TABLES%20';        %20}        %20$result%20=  %20$this->query($sql);        %20$info  %20=  %20array();        %20foreach%20($result%20as%20$key%20=>%20$val)%20{            %20$info[$key]%20=%20current($val);        %20}        %20return%20$info;    %20}     %20/**    %20+----------------------------------------------------------    %20*%20最后次操作的ID    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/     %20public%20function%20last_insert_id(){        %20return%20mysql_insert_id($this->conn);    %20}    %20/**    %20*%20执行一条带有结果集计数的    %20*/    %20public%20function%20count($sql){        %20return%20$this->execute($sql);    %20}    %20/**    %20+----------------------------------------------------------    %20*%20启动事务    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@return%20void    %20+----------------------------------------------------------    %20*/    %20public%20function%20startTrans()%20{        %20if%20($this->transTimes%20==%200)%20{            %20mysql_query('START%20TRANSACTION',%20$this->conn);        %20}        %20$this->transTimes++;        %20return%20;    %20}     %20/**    %20+----------------------------------------------------------    %20*%20提交事务    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@return%20boolen    %20+----------------------------------------------------------    %20*/    %20public%20function%20commit()    %20{        %20if%20($this->transTimes%20>%200)%20{            %20$result%20=%20mysql_query('COMMIT',%20$this->conn);            %20$this->transTimes%20=%200;            %20if(!$result){                %20throw%20new%20Exception($this->error());            %20}        %20}        %20return%20true;    %20}     %20/**    %20+----------------------------------------------------------    %20*%20事务回滚    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@return%20boolen    %20+----------------------------------------------------------    %20*/    %20public%20function%20rollback()    %20{        %20if%20($this->transTimes%20>%200)%20{            %20$result%20=%20mysql_query('ROLLBACK',%20$this->conn);            %20$this->transTimes%20=%200;            %20if(!$result){                %20throw%20new%20Exception($this->error());            %20}        %20}        %20return%20true;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20错误信息    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/     %20public%20function%20error()%20{        %20$this->error%20=%20mysql_error($this->conn);        %20if(''%20!=%20$this->queryStr){            %20$this->error%20.=%20"/n%20[%20SQL语句%20]%20:%20".$this->queryStr;        %20}        %20return%20$this->error;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20释放查询结果    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/    %20public%20function%20free()%20{        %20@mysql_free_result($this->queryID);        %20$this->queryID%20=%200;        %20$this->query_list%20=%20null;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20关闭连接    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param     %20+----------------------------------------------------------    %20*/    %20function%20close(){        %20if%20($this->conn%20&&%20!mysql_close($this->conn)){            %20throw%20new%20Exception($this->error());        %20}        %20$this->conn%20=%200;        %20$this->query_count%20=%200;    %20}    %20/**    %20+----------------------------------------------------------    %20*%20析构方法    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/    %20function%20__destruct(){         %20$this->close();    %20} 

<?php//%20+----------------------------------------------------------------------//%20|MySQL操作类//%20+----------------------------------------------------------------------//%20|@微凉%20QQ:496928838//%20+----------------------------------------------------------------------class%20MySQL{  private%20$db_mysql_hostname; private%20$db_mysql_username; private%20$db_mysql_password; private%20$db_mysql_database; private%20$db_mysql_port; private%20$db_mysql_charset;  private%20$query_list%20=%20array();  //查询次数 public%20$query_count%20=%200; //查询开始时间 public%20$query_start_time;  //当前查询ID protected%20$queryID; //当前连接 protected%20$conn; //%20事务指令数 protected%20$transTimes%20=%200; //%20返回或者影响记录数   %20protected%20$numRows   %20=%200;   %20//%20错误信息   %20protected%20$error     %20=%20'';  public%20function%20__construct($hostname_or_conf,$username,$password,$database,$port%20=%20'3306',$char%20=%20'utf8'){  if(is_array($hostname_or_conf)){   $this->db_mysql_hostname%20=%20$hostname_or_conf['hostname'];   $this->db_mysql_username%20=%20$hostname_or_conf['username'];   $this->db_mysql_password%20=%20$hostname_or_conf['password'];   $this->db_mysql_database%20=%20$hostname_or_conf['database'];   $this->db_mysql_port%20=%20isset($hostname_or_conf['port'])?$hostname_or_conf['port']:'3306';   $this->db_mysql_charset%20=%20isset($hostname_or_conf['charset'])?$hostname_or_conf['charset']:'utf8';     }elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database))  {   %20$this->db_mysql_hostname%20=%20$hostname_or_conf;  %20  %20$this->db_mysql_username%20=%20$username;  %20  %20$this->db_mysql_password%20=%20$password; %20  %20$this->db_mysql_database%20=%20$database;   %20$this->db_mysql_port%20=%20$port;   %20$this->db_mysql_charset%20=%20$char;     }else{   die('configuration%20error.');  }     %20 $this->connect();   %20}  private%20function%20connect(){  $server%20=%20$this->db_mysql_hostname.':'.$this->db_mysql_port;  $this->conn%20=%20mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true)%20or%20die('Connect%20MySQL%20DB%20error!');  mysql_select_db($this->db_mysql_database,$this->conn)%20or%20die('select%20db%20error!');  mysql_query("set%20names%20"%20.%20$this->db_mysql_charset,%20$this->conn); } /**    %20+----------------------------------------------------------    %20*%20设置数据对象值    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*table,where,order,limit,data,field,join,group,having    %20+----------------------------------------------------------    %20*/ public%20function%20table($table){  $this->query_list['table']%20=%20$table;  return%20$this; }  public%20function%20where($where){  $this->query_list['where']%20=%20$where;  return%20$this; }  public%20function%20order($order){  $this->query_list['order']%20=%20$order;  return%20$this; }  public%20function%20limit($offset,$length){  if(!isset($length)){   $length%20=%20$offset;   $offset%20=%200;  }  $this->query_list['limit']%20=%20'limit%20'.$offset.','.$length;  return%20$this; }  public%20function%20data($data){  //读取数据表字段,然后处理表单数据  $dataList%20=%20$this->getFields($this->query_list['table']);  $arr=array();  foreach%20($dataList%20as%20$key=>$value)%20{   if%20(array_key_exists%20($key,$data)%20)%20{    $arr[$key]=$data[$key];   }     }  //var_dump($arr);  /*  if(is_object($data)){   $data  %20=  %20get_object_vars($data);  }elseif%20(is_string($data)){   parse_str($data,$data);  }elseif(!is_array($data)){   //Log:DATA_TYPE_INVALID  }  */  $this->query_list['data']%20=%20$arr;  return%20$this; } public%20function%20field($fields){  $this->query_list['fields']%20=%20$fields;  return%20$this; } public%20function%20join($join){  $this->query_list['join']%20=%20$join;  return%20$this; } public%20function%20group($group){  $this->query_list['group']%20=%20$group;  return%20$this; } public%20function%20having($having){  $this->query_list['having']%20=%20$having;  return%20$this; } /**    %20+----------------------------------------------------------    %20*%20查询    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ public%20function%20select(){  $select_sql%20=%20'select%20';  $fields%20=%20isset($this->query_list['fields'])?$this->query_list['fields']:'*';  $select_sql.=$fields;  $select_sql.=%20'%20from%20`'.$this->query_list['table'].'`%20';    isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):'';  isset($this->query_list['where'])?($select_sql.='%20where%20'.$this->query_list['where']):'';  isset($this->query_list['group'])?($select_sql.='%20group%20by'.$this->query_list['group']):'';  isset($this->query_list['having'])?($select_sql.='%20mysql%20having%20'.$this->query_list['having']):'';  isset($this->query_list['order'])?($select_sql.='%20order%20by%20'.$this->query_list['order']):'';  isset($this->query_list['limit'])?($select_sql.='%20'.$this->query_list['limit']):'';    return%20$this->query($select_sql); } /**    %20+----------------------------------------------------------    %20*%20增加    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ public%20function%20add(){  $add_sql%20=%20'insert%20into%20`'.$this->query_list['table'].'`%20(';    $data%20=%20$this->query_list['data'];  $value%20=%20$field%20=%20'';  foreach($data%20as%20$k=>$v){   $field%20.=%20'`'.$k.'`,';   if(is_numeric($v))    $value%20.=%20$v.',';   else    $value%20.=%20'/''.$v.'/',';  }  $add_sql%20.=%20rtrim($field,',').')%20values%20('.rtrim($value,',').')';

 // echo%20'add_sql'.$add_sql;  return%20$this->execute($add_sql); } /**    %20+----------------------------------------------------------    %20*%20删除    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ public%20function%20delete(){  $del_sql%20=%20'delete%20from%20`'.$this->query_list['table'].'`%20where%20'.$this->query_list['where'];    if(isset($this->query_list['order']))   $del_sql%20.=%20'order%20by%20'.$this->query_list['order'];  if(isset($this->query_list['limit']))   $del_sql%20.=%20'%20'.$this->query_list['limit'];     return%20$this->execute($del_sql);   } /**    %20+----------------------------------------------------------    %20*%20更新    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ public%20function%20update(){  $update_sql%20=%20'update%20`'.$this->query_list['table'].'`%20set%20';  $data%20=%20$this->query_list['data'];    foreach($data%20as%20$k=>$v){   if(is_numeric($v))    $update_sql%20.=%20'`'.$k.'`%20='.$v.',';   else    $update_sql%20.=%20'`'.$k.'`%20=/''.$v.'/',';  }  $update_sql%20=%20rtrim($update_sql,',');  if(isset($this->query_list['where']))   $update_sql%20.=%20'%20where%20'.$this->query_list['where'];  if(isset($this->query_list['order']))   $update_sql%20.=%20'%20order%20by%20'.$this->query_list['order'];  if(isset($this->query_list['limit']))   $update_sql%20.=%20'%20'.$this->query_list['limit'];    return%20$this->execute($update_sql);   } %20/**    %20+----------------------------------------------------------    %20*%20执行查询%20返回数据集    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param%20string%20$sql %20sql指令    %20*/   %20public%20function%20query($sql)%20{       %20if%20(%20!$this->conn%20)%20return%20false;       %20$this->queryStr%20=%20$sql;       %20//释放前次的查询结果       %20if%20(%20$this->queryID%20)%20{   %20$this->free();   %20}              %20$this->query_start_time%20=%20microtime(true);              %20$this->queryID%20=%20mysql_query($sql,%20$this->conn);       %20$this->query_count++;       %20if%20(%20false%20===%20$this->queryID%20)%20{           %20$this->error();           %20return%20false;       %20}%20else%20{           %20$this->numRows%20=%20mysql_num_rows($this->queryID);           %20return%20$this->getAll();       %20}   %20} /**    %20+----------------------------------------------------------    %20*%20执行语句    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param%20string%20$sql %20sql指令    %20+----------------------------------------------------------    %20*/   %20public%20function%20execute($sql)%20{       %20if%20(%20!$this->conn%20)%20return%20false;       %20$this->queryStr%20=%20$sql;       %20//释放前次的查询结果       %20if%20(%20$this->queryID%20)%20{   %20$this->free();   %20}              %20$this->query_start_time%20=%20microtime(true);              %20$result%20=  %20mysql_query($sql,%20$this->conn)%20;       %20$this->query_count++;       %20if%20(%20false%20===%20$result)%20{           %20$this->error();           %20return%20false;       %20}%20else%20{           %20$this->numRows%20=%20mysql_affected_rows($this->conn);           %20return%20$this->numRows;       %20}   %20} /**    %20+----------------------------------------------------------    %20*%20获得所有的查询数据    %20+----------------------------------------------------------    %20*%20@access%20private    %20+----------------------------------------------------------    %20*%20@return%20array    %20*/   %20private%20function%20getAll()%20{       %20//返回数据集       %20$result%20=%20array();       %20if($this->numRows%20>0)%20{           %20while($row%20=%20mysql_fetch_assoc($this->queryID)){               %20$result[]  %20=  %20$row;           %20}           %20mysql_data_seek($this->queryID,0);       %20}       %20return%20$result;   %20} /**    %20+----------------------------------------------------------    %20*%20取得数据表的字段信息    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/   %20public%20function%20getFields($tableName)%20{       %20$result%20=  %20$this->query('SHOW%20COLUMNS%20FROM%20`'.$tableName.'`');       %20$info  %20=  %20array();       %20if($result)%20{           %20foreach%20($result%20as%20$key%20=>%20$val)%20{               %20$info[$val['Field']]%20=%20array(                   %20'name'   %20=>%20$val['Field'],                   %20'type'   %20=>%20$val['Type'],                   %20'notnull'%20=>%20(bool)%20($val['Null']%20===%20''),%20//%20not%20null%20is%20empty,%20null%20is%20yes                   %20'default'%20=>%20$val['Default'],                   %20'primary'%20=>%20(strtolower($val['Key'])%20==%20'pri'),                   %20'autoinc'%20=>%20(strtolower($val['Extra'])%20==%20'auto_increment'),               %20);           %20}       %20}       %20return%20$info;   %20} /**    %20+----------------------------------------------------------    %20*%20取得数据库的表信息    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/   %20public%20function%20getTables($dbName='')%20{       %20if(!empty($dbName))%20{          %20$sql   %20=%20'SHOW%20TABLES%20FROM%20'.$dbName;       %20}else{          %20$sql   %20=%20'SHOW%20TABLES%20';       %20}       %20$result%20=  %20$this->query($sql);       %20$info  %20=  %20array();       %20foreach%20($result%20as%20$key%20=>%20$val)%20{           %20$info[$key]%20=%20current($val);       %20}       %20return%20$info;   %20}

 /**    %20+----------------------------------------------------------    %20*%20最后次操作的ID    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ %20public%20function%20last_insert_id(){       %20return%20mysql_insert_id($this->conn);   %20}   %20/**    %20*%20执行一条带有结果集计数的    %20*/   %20public%20function%20count($sql){       %20return%20$this->execute($sql);   %20} /**    %20+----------------------------------------------------------    %20*%20启动事务    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@return%20void    %20+----------------------------------------------------------    %20*/   %20public%20function%20startTrans()%20{       %20if%20($this->transTimes%20==%200)%20{           %20mysql_query('START%20TRANSACTION',%20$this->conn);       %20}       %20$this->transTimes++;       %20return%20;   %20}

   %20/**    %20+----------------------------------------------------------    %20*%20提交事务    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@return%20boolen    %20+----------------------------------------------------------    %20*/   %20public%20function%20commit()   %20{       %20if%20($this->transTimes%20>%200)%20{           %20$result%20=%20mysql_query('COMMIT',%20$this->conn);           %20$this->transTimes%20=%200;           %20if(!$result){               %20throw%20new%20Exception($this->error());           %20}       %20}       %20return%20true;   %20}

   %20/**    %20+----------------------------------------------------------    %20*%20事务回滚    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@return%20boolen    %20+----------------------------------------------------------    %20*/   %20public%20function%20rollback()   %20{       %20if%20($this->transTimes%20>%200)%20{           %20$result%20=%20mysql_query('ROLLBACK',%20$this->conn);           %20$this->transTimes%20=%200;           %20if(!$result){               %20throw%20new%20Exception($this->error());           %20}       %20}       %20return%20true;   %20} /**    %20+----------------------------------------------------------    %20*%20错误信息    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ %20public%20function%20error()%20{       %20$this->error%20=%20mysql_error($this->conn);       %20if(''%20!=%20$this->queryStr){           %20$this->error%20.=%20"/n%20[%20SQL语句%20]%20:%20".$this->queryStr;       %20}       %20return%20$this->error;   %20} /**    %20+----------------------------------------------------------    %20*%20释放查询结果    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/   %20public%20function%20free()%20{       %20@mysql_free_result($this->queryID);       %20$this->queryID%20=%200;       %20$this->query_list%20=%20null;   %20}   %20/**    %20+----------------------------------------------------------    %20*%20关闭连接    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*%20@param    %20+----------------------------------------------------------    %20*/ function%20close(){  if%20($this->conn%20&&%20!mysql_close($this->conn)){           %20throw%20new%20Exception($this->error());       %20}       %20$this->conn%20=%200;       %20$this->query_count%20=%200; } /**    %20+----------------------------------------------------------    %20*%20析构方法    %20+----------------------------------------------------------    %20*%20@access%20public    %20+----------------------------------------------------------    %20*/ function%20__destruct(){  %20$this->close(); }}3.我学会了浏览器重定向

用HTTP头信息PHP里的header()函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("Context-type:%20xxx/xxx"),页面的属性("No%20cache",%20"Expire")等等。用HTTP头信息重定向到另外一个页面的方法如下:

[php] %20<?php Header("HTTP/1.1%20303%20See%20Other"); Header("Location:%20$url");  %20//注意Location:%20后面有一个空格  exit; ?> 

<?phpHeader("HTTP/1.1%20303%20See%20Other");Header("Location:%20$url");  %20//注意Location:%20后面有一个空格exit;}?>用脚本来实现举例如下:[javascript]%20<?php $url="http://localhost/"; echo%20"<!--<SCRIPT%20LANGUAGE="JavaScript">"; echo%20"location.href=’$url’"; echo%20"</SCRIPT>-->"; ?> 

<?php$url="http://localhost/";echo%20"<!--<SCRIPT%20LANGUAGE="JavaScript">";echo%20"location.href=’$url’";echo%20"</SCRIPT>-->";?>

 

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜