1.我的留言板上寫上了 add方法
[php] function add(){
//添加後返回 影響條數,如果大於0就說明添加成功
if($this->db->data($_POST)->add()>0){
echo "添加成功";
// echo "<script>location.reload()</script>";//防止刷新後的表單的重復提交
Header("HTTP/1.1 303 See Other");
Header("Location: "); //轉向到根目錄
exit;
}else {
die($this->db->error());//添加失敗輸出錯誤信息
}
}
function add(){
//添加後返回 影響條數,如果大於0就說明添加成功
if($this->db->data($_POST)->add()>0){
echo "添加成功";
// echo "<script>location.reload()</script>";//防止刷新後的表單的重復提交
Header("HTTP/1.1 303 See Other");
Header("Location: "); //轉向到根目錄
exit;
}else {
die($this->db->error());//添加失敗輸出錯誤信息
}
}對應提交表單
[html] <div class="center">
<form action="?m=index&a=add" method="post">
<table border=0>
<tr>
<td>用戶名</td>
<td><input type="text" name="userName" value="admin"></td>
</tr>
<tr>
<td>留言內容</td>
<td><textarea rows="3" cols="20" name="content">aaaaa</textarea></td>
</tr>
<tr>
<td>電子郵箱</td>
<td>email:<input type="text" name="email" value="[email protected]"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="留言"></td>
</tr>
</table>
<input type="hidden" name="action" value="add">
</form>
</div>
</div>
<div class="center">
<form action="?m=index&a=add" method="post">
<table border=0>
<tr>
<td>用戶名</td>
<td><input type="text" name="userName" value="admin"></td>
</tr>
<tr>
<td>留言內容</td>
<td><textarea rows="3" cols="20" name="content">aaaaa</textarea></td>
</tr>
<tr>
<td>電子郵箱</td>
<td>email:<input type="text" name="email" value="[email protected]"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="留言"></td>
</tr>
</table>
<input type="hidden" name="action" value="add">
</form>
</div>
</div>
2.修改了MYSQL操作類 使 data 自動處理傳遞過來的數據,對比是否存在字段,不存在不添加.
[php] <?php
// +----------------------------------------------------------------------
// |MySQL操作類
// +----------------------------------------------------------------------
// |@微涼 QQ:496928838
// +----------------------------------------------------------------------
class MySQL{
private $db_mysql_hostname;
private $db_mysql_username;
private $db_mysql_password;
private $db_mysql_database;
private $db_mysql_port;
private $db_mysql_charset;
private $query_list = array();
//查詢次數
public $query_count = 0;
//查詢開始時間
public $query_start_time;
//當前查詢ID
protected $queryID;
//當前連接
protected $conn;
// 事務指令數
protected $transTimes = 0;
// 返回或者影響記錄數
protected $numRows = 0;
// 錯誤信息
protected $error = '';
public function __construct($hostname_or_conf,$username,$password,$database,$port = '3306',$char = 'utf8'){
if(is_array($hostname_or_conf)){
$this->db_mysql_hostname = $hostname_or_conf['hostname'];
$this->db_mysql_username = $hostname_or_conf['username'];
$this->db_mysql_password = $hostname_or_conf['password'];
$this->db_mysql_database = $hostname_or_conf['database'];
$this->db_mysql_port = isset($hostname_or_conf['port'])?$hostname_or_conf['port']:'3306';
$this->db_mysql_charset = isset($hostname_or_conf['charset'])?$hostname_or_conf['charset']:'utf8';
}elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database))
{
$this->db_mysql_hostname = $hostname_or_conf;
$this->db_mysql_username = $username;
$this->db_mysql_password = $password;
$this->db_mysql_database = $database;
$this->db_mysql_port = $port;
$this->db_mysql_charset = $char;
}else{
die('configuration error.');
}
$this->connect();
}
private function connect(){
$server = $this->db_mysql_hostname.':'.$this->db_mysql_port;
$this->conn = mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true) or die('Connect MySQL DB error!');
mysql_select_db($this->db_mysql_database,$this->conn) or die('select db error!');
mysql_query("set names " . $this->db_mysql_charset, $this->conn);
}
/**
+----------------------------------------------------------
* 設置數據對象值
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*table,where,order,limit,data,field,join,group,having
+----------------------------------------------------------
*/
public function table($table){
$this->query_list['table'] = $table;
return $this;
}
public function where($where){
$this->query_list['where'] = $where;
return $this;
}
public function order($order){
$this->query_list['order'] = $order;
return $this;
}
public function limit($offset,$length){
if(!isset($length)){
$length = $offset;
$offset = 0;
}
$this->query_list['limit'] = 'limit '.$offset.','.$length;
return $this;
}
public function data($data){
//讀取數據表字段,然後處理表單數據
$dataList = $this->getFields($this->query_list['table']);
$arr=array();
foreach ($dataList as $key=>$value) {
if (array_key_exists ($key,$data) ) {
$arr[$key]=$data[$key];
}
}
//var_dump($arr);
/*
if(is_object($data)){
$data = get_object_vars($data);
}elseif (is_string($data)){
parse_str($data,$data);
}elseif(!is_array($data)){
//Log:DATA_TYPE_INVALID
}
*/
$this->query_list['data'] = $arr;
return $this;
}
public function field($fields){
$this->query_list['fields'] = $fields;
return $this;
}
public function join($join){
$this->query_list['join'] = $join;
return $this;
}
public function group($group){
$this->query_list['group'] = $group;
return $this;
}
public function having($having){
$this->query_list['having'] = $having;
return $this;
}
/**
+----------------------------------------------------------
* 查詢
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function select(){
$select_sql = 'select ';
$fields = isset($this->query_list['fields'])?$this->query_list['fields']:'*';
$select_sql.=$fields;
$select_sql.= ' from `'.$this->query_list['table'].'` ';
isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):'';
isset($this->query_list['where'])?($select_sql.=' where '.$this->query_list['where']):'';
isset($this->query_list['group'])?($select_sql.=' group by'.$this->query_list['group']):'';
isset($this->query_list['having'])?($select_sql.=' mysql having '.$this->query_list['having']):'';
isset($this->query_list['order'])?($select_sql.=' order by '.$this->query_list['order']):'';
isset($this->query_list['limit'])?($select_sql.=' '.$this->query_list['limit']):'';
return $this->query($select_sql);
}
/**
+----------------------------------------------------------
* 增加
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function add(){
$add_sql = 'insert into `'.$this->query_list['table'].'` (';
$data = $this->query_list['data'];
$value = $field = '';
foreach($data as $k=>$v){
$field .= '`'.$k.'`,';
if(is_numeric($v))
$value .= $v.',';
else
$value .= '\''.$v.'\',';
}
$add_sql .= rtrim($field,',').') values ('.rtrim($value,',').')';
// echo 'add_sql'.$add_sql;
return $this->execute($add_sql);
}
/**
+----------------------------------------------------------
* 刪除
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function delete(){
$del_sql = 'delete from `'.$this->query_list['table'].'` where '.$this->query_list['where'];
if(isset($this->query_list['order']))
$del_sql .= 'order by '.$this->query_list['order'];
if(isset($this->query_list['limit']))
$del_sql .= ' '.$this->query_list['limit'];
return $this->execute($del_sql);
}
/**
+----------------------------------------------------------
* 更新
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function update(){
$update_sql = 'update `'.$this->query_list['table'].'` set ';
$data = $this->query_list['data'];
foreach($data as $k=>$v){
if(is_numeric($v))
$update_sql .= '`'.$k.'` ='.$v.',';
else
$update_sql .= '`'.$k.'` =\''.$v.'\',';
}
$update_sql = rtrim($update_sql,',');
if(isset($this->query_list['where']))
$update_sql .= ' where '.$this->query_list['where'];
if(isset($this->query_list['order']))
$update_sql .= ' order by '.$this->query_list['order'];
if(isset($this->query_list['limit']))
$update_sql .= ' '.$this->query_list['limit'];
return $this->execute($update_sql);
}
/**
+----------------------------------------------------------
* 執行查詢 返回數據集
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql sql指令
*/
public function query($sql) {
if ( !$this->conn ) return false;
$this->queryStr = $sql;
//釋放前次的查詢結果
if ( $this->queryID ) { $this->free(); }
$this->query_start_time = microtime(true);
$this->queryID = mysql_query($sql, $this->conn);
$this->query_count++;
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = mysql_num_rows($this->queryID);
return $this->getAll();
}
}
/**
+----------------------------------------------------------
* 執行語句
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql sql指令
+----------------------------------------------------------
*/
public function execute($sql) {
if ( !$this->conn ) return false;
$this->queryStr = $sql;
//釋放前次的查詢結果
if ( $this->queryID ) { $this->free(); }
$this->query_start_time = microtime(true);
$result = mysql_query($sql, $this->conn) ;
$this->query_count++;
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = mysql_affected_rows($this->conn);
return $this->numRows;
}
}
/**
+----------------------------------------------------------
* 獲得所有的查詢數據
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @return array
*/
private function getAll() {
//返回數據集
$result = array();
if($this->numRows >0) {
while($row = mysql_fetch_assoc($this->queryID)){
$result[] = $row;
}
mysql_data_seek($this->queryID,0);
}
return $result;
}
/**
+----------------------------------------------------------
* 取得數據表的字段信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function getFields($tableName) {
$result = $this->query('SHOW COLUMNS FROM `'.$tableName.'`');
$info = array();
if($result) {
foreach ($result as $key => $val) {
$info[$val['Field']] = array(
'name' => $val['Field'],
'type' => $val['Type'],
'notnull' => (bool) ($val['Null'] === ''), // not null is empty, null is yes
'default' => $val['Default'],
'primary' => (strtolower($val['Key']) == 'pri'),
'autoinc' => (strtolower($val['Extra']) == 'auto_increment'),
);
}
}
return $info;
}
/**
+----------------------------------------------------------
* 取得數據庫的表信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function getTables($dbName='') {
if(!empty($dbName)) {
$sql = 'SHOW TABLES FROM '.$dbName;
}else{
$sql = 'SHOW TABLES ';
}
$result = $this->query($sql);
$info = array();
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
return $info;
}
/**
+----------------------------------------------------------
* 最後次操作的ID
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function last_insert_id(){
return mysql_insert_id($this->conn);
}
/**
* 執行一條帶有結果集計數的
*/
public function count($sql){
return $this->execute($sql);
}
/**
+----------------------------------------------------------
* 啟動事務
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
public function startTrans() {
if ($this->transTimes == 0) {
mysql_query('START TRANSACTION', $this->conn);
}
$this->transTimes++;
return ;
}
/**
+----------------------------------------------------------
* 提交事務
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function commit()
{
if ($this->transTimes > 0) {
$result = mysql_query('COMMIT', $this->conn);
$this->transTimes = 0;
if(!$result){
throw new Exception($this->error());
}
}
return true;
}
/**
+----------------------------------------------------------
* 事務回滾
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function rollback()
{
if ($this->transTimes > 0) {
$result = mysql_query('ROLLBACK', $this->conn);
$this->transTimes = 0;
if(!$result){
throw new Exception($this->error());
}
}
return true;
}
/**
+----------------------------------------------------------
* 錯誤信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function error() {
$this->error = mysql_error($this->conn);
if('' != $this->queryStr){
$this->error .= "\n [ SQL語句 ] : ".$this->queryStr;
}
return $this->error;
}
/**
+----------------------------------------------------------
* 釋放查詢結果
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function free() {
@mysql_free_result($this->queryID);
$this->queryID = 0;
$this->query_list = null;
}
/**
+----------------------------------------------------------
* 關閉連接
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
function close(){
if ($this->conn && !mysql_close($this->conn)){
throw new Exception($this->error());
}
$this->conn = 0;
$this->query_count = 0;
}
/**
+----------------------------------------------------------
* 析構方法
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
function __destruct(){
$this->close();
}
}
<?php
// +----------------------------------------------------------------------
// |MySQL操作類
// +----------------------------------------------------------------------
// |@微涼 QQ:496928838
// +----------------------------------------------------------------------
class MySQL{
private $db_mysql_hostname;
private $db_mysql_username;
private $db_mysql_password;
private $db_mysql_database;
private $db_mysql_port;
private $db_mysql_charset;
private $query_list = array();
//查詢次數
public $query_count = 0;
//查詢開始時間
public $query_start_time;
//當前查詢ID
protected $queryID;
//當前連接
protected $conn;
// 事務指令數
protected $transTimes = 0;
// 返回或者影響記錄數
protected $numRows = 0;
// 錯誤信息
protected $error = '';
public function __construct($hostname_or_conf,$username,$password,$database,$port = '3306',$char = 'utf8'){
if(is_array($hostname_or_conf)){
$this->db_mysql_hostname = $hostname_or_conf['hostname'];
$this->db_mysql_username = $hostname_or_conf['username'];
$this->db_mysql_password = $hostname_or_conf['password'];
$this->db_mysql_database = $hostname_or_conf['database'];
$this->db_mysql_port = isset($hostname_or_conf['port'])?$hostname_or_conf['port']:'3306';
$this->db_mysql_charset = isset($hostname_or_conf['charset'])?$hostname_or_conf['charset']:'utf8';
}elseif(!empty($hostname_or_conf)||!empty($username)||!empty($password)||!empty($database))
{
$this->db_mysql_hostname = $hostname_or_conf;
$this->db_mysql_username = $username;
$this->db_mysql_password = $password;
$this->db_mysql_database = $database;
$this->db_mysql_port = $port;
$this->db_mysql_charset = $char;
}else{
die('configuration error.');
}
$this->connect();
}
private function connect(){
$server = $this->db_mysql_hostname.':'.$this->db_mysql_port;
$this->conn = mysql_connect($server,$this->db_mysql_username,$this->db_mysql_password,true) or die('Connect MySQL DB error!');
mysql_select_db($this->db_mysql_database,$this->conn) or die('select db error!');
mysql_query("set names " . $this->db_mysql_charset, $this->conn);
}
/**
+----------------------------------------------------------
* 設置數據對象值
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*table,where,order,limit,data,field,join,group,having
+----------------------------------------------------------
*/
public function table($table){
$this->query_list['table'] = $table;
return $this;
}
public function where($where){
$this->query_list['where'] = $where;
return $this;
}
public function order($order){
$this->query_list['order'] = $order;
return $this;
}
public function limit($offset,$length){
if(!isset($length)){
$length = $offset;
$offset = 0;
}
$this->query_list['limit'] = 'limit '.$offset.','.$length;
return $this;
}
public function data($data){
//讀取數據表字段,然後處理表單數據
$dataList = $this->getFields($this->query_list['table']);
$arr=array();
foreach ($dataList as $key=>$value) {
if (array_key_exists ($key,$data) ) {
$arr[$key]=$data[$key];
}
}
//var_dump($arr);
/*
if(is_object($data)){
$data = get_object_vars($data);
}elseif (is_string($data)){
parse_str($data,$data);
}elseif(!is_array($data)){
//Log:DATA_TYPE_INVALID
}
*/
$this->query_list['data'] = $arr;
return $this;
}
public function field($fields){
$this->query_list['fields'] = $fields;
return $this;
}
public function join($join){
$this->query_list['join'] = $join;
return $this;
}
public function group($group){
$this->query_list['group'] = $group;
return $this;
}
public function having($having){
$this->query_list['having'] = $having;
return $this;
}
/**
+----------------------------------------------------------
* 查詢
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function select(){
$select_sql = 'select ';
$fields = isset($this->query_list['fields'])?$this->query_list['fields']:'*';
$select_sql.=$fields;
$select_sql.= ' from `'.$this->query_list['table'].'` ';
isset($this->query_list['join'])?($select_sql.=$this->query_list['join']):'';
isset($this->query_list['where'])?($select_sql.=' where '.$this->query_list['where']):'';
isset($this->query_list['group'])?($select_sql.=' group by'.$this->query_list['group']):'';
isset($this->query_list['having'])?($select_sql.=' mysql having '.$this->query_list['having']):'';
isset($this->query_list['order'])?($select_sql.=' order by '.$this->query_list['order']):'';
isset($this->query_list['limit'])?($select_sql.=' '.$this->query_list['limit']):'';
return $this->query($select_sql);
}
/**
+----------------------------------------------------------
* 增加
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function add(){
$add_sql = 'insert into `'.$this->query_list['table'].'` (';
$data = $this->query_list['data'];
$value = $field = '';
foreach($data as $k=>$v){
$field .= '`'.$k.'`,';
if(is_numeric($v))
$value .= $v.',';
else
$value .= '\''.$v.'\',';
}
$add_sql .= rtrim($field,',').') values ('.rtrim($value,',').')';
// echo 'add_sql'.$add_sql;
return $this->execute($add_sql);
}
/**
+----------------------------------------------------------
* 刪除
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function delete(){
$del_sql = 'delete from `'.$this->query_list['table'].'` where '.$this->query_list['where'];
if(isset($this->query_list['order']))
$del_sql .= 'order by '.$this->query_list['order'];
if(isset($this->query_list['limit']))
$del_sql .= ' '.$this->query_list['limit'];
return $this->execute($del_sql);
}
/**
+----------------------------------------------------------
* 更新
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function update(){
$update_sql = 'update `'.$this->query_list['table'].'` set ';
$data = $this->query_list['data'];
foreach($data as $k=>$v){
if(is_numeric($v))
$update_sql .= '`'.$k.'` ='.$v.',';
else
$update_sql .= '`'.$k.'` =\''.$v.'\',';
}
$update_sql = rtrim($update_sql,',');
if(isset($this->query_list['where']))
$update_sql .= ' where '.$this->query_list['where'];
if(isset($this->query_list['order']))
$update_sql .= ' order by '.$this->query_list['order'];
if(isset($this->query_list['limit']))
$update_sql .= ' '.$this->query_list['limit'];
return $this->execute($update_sql);
}
/**
+----------------------------------------------------------
* 執行查詢 返回數據集
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql sql指令
*/
public function query($sql) {
if ( !$this->conn ) return false;
$this->queryStr = $sql;
//釋放前次的查詢結果
if ( $this->queryID ) { $this->free(); }
$this->query_start_time = microtime(true);
$this->queryID = mysql_query($sql, $this->conn);
$this->query_count++;
if ( false === $this->queryID ) {
$this->error();
return false;
} else {
$this->numRows = mysql_num_rows($this->queryID);
return $this->getAll();
}
}
/**
+----------------------------------------------------------
* 執行語句
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql sql指令
+----------------------------------------------------------
*/
public function execute($sql) {
if ( !$this->conn ) return false;
$this->queryStr = $sql;
//釋放前次的查詢結果
if ( $this->queryID ) { $this->free(); }
$this->query_start_time = microtime(true);
$result = mysql_query($sql, $this->conn) ;
$this->query_count++;
if ( false === $result) {
$this->error();
return false;
} else {
$this->numRows = mysql_affected_rows($this->conn);
return $this->numRows;
}
}
/**
+----------------------------------------------------------
* 獲得所有的查詢數據
+----------------------------------------------------------
* @access private
+----------------------------------------------------------
* @return array
*/
private function getAll() {
//返回數據集
$result = array();
if($this->numRows >0) {
while($row = mysql_fetch_assoc($this->queryID)){
$result[] = $row;
}
mysql_data_seek($this->queryID,0);
}
return $result;
}
/**
+----------------------------------------------------------
* 取得數據表的字段信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function getFields($tableName) {
$result = $this->query('SHOW COLUMNS FROM `'.$tableName.'`');
$info = array();
if($result) {
foreach ($result as $key => $val) {
$info[$val['Field']] = array(
'name' => $val['Field'],
'type' => $val['Type'],
'notnull' => (bool) ($val['Null'] === ''), // not null is empty, null is yes
'default' => $val['Default'],
'primary' => (strtolower($val['Key']) == 'pri'),
'autoinc' => (strtolower($val['Extra']) == 'auto_increment'),
);
}
}
return $info;
}
/**
+----------------------------------------------------------
* 取得數據庫的表信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function getTables($dbName='') {
if(!empty($dbName)) {
$sql = 'SHOW TABLES FROM '.$dbName;
}else{
$sql = 'SHOW TABLES ';
}
$result = $this->query($sql);
$info = array();
foreach ($result as $key => $val) {
$info[$key] = current($val);
}
return $info;
}
/**
+----------------------------------------------------------
* 最後次操作的ID
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function last_insert_id(){
return mysql_insert_id($this->conn);
}
/**
* 執行一條帶有結果集計數的
*/
public function count($sql){
return $this->execute($sql);
}
/**
+----------------------------------------------------------
* 啟動事務
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
public function startTrans() {
if ($this->transTimes == 0) {
mysql_query('START TRANSACTION', $this->conn);
}
$this->transTimes++;
return ;
}
/**
+----------------------------------------------------------
* 提交事務
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function commit()
{
if ($this->transTimes > 0) {
$result = mysql_query('COMMIT', $this->conn);
$this->transTimes = 0;
if(!$result){
throw new Exception($this->error());
}
}
return true;
}
/**
+----------------------------------------------------------
* 事務回滾
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
public function rollback()
{
if ($this->transTimes > 0) {
$result = mysql_query('ROLLBACK', $this->conn);
$this->transTimes = 0;
if(!$result){
throw new Exception($this->error());
}
}
return true;
}
/**
+----------------------------------------------------------
* 錯誤信息
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
public function error() {
$this->error = mysql_error($this->conn);
if('' != $this->queryStr){
$this->error .= "\n [ SQL語句 ] : ".$this->queryStr;
}
return $this->error;
}
/**
+----------------------------------------------------------
* 釋放查詢結果
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
public function free() {
@mysql_free_result($this->queryID);
$this->queryID = 0;
$this->query_list = null;
}
/**
+----------------------------------------------------------
* 關閉連接
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param
+----------------------------------------------------------
*/
function close(){
if ($this->conn && !mysql_close($this->conn)){
throw new Exception($this->error());
}
$this->conn = 0;
$this->query_count = 0;
}
/**
+----------------------------------------------------------
* 析構方法
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
*/
function __destruct(){
$this->close();
}
}
3.我學會了浏覽器重定向
用HTTP頭信息
PHP裡的header()函數的作用就是向浏覽器發出由HTTP協議規定的本來應該通過WEB服務器的控制指令,例如聲明返回信息的類型("Context-type: xxx/xxx"),頁面的屬性("No cache", "Expire")等等。
用HTTP頭信息重定向到另外一個頁面的方法如下:
[php] <?php
Header("HTTP/1.1 303 See Other");
Header("Location: $url"); //注意Location: 後面有一個空格
exit;
}
?>
<?php
Header("HTTP/1.1 303 See Other");
Header("Location: $url"); //注意Location: 後面有一個空格
exit;
}
?>用腳本來實現
舉例如下:
[javascript] <?php
$url="http://localhost/";
echo "<!--<SCRIPT LANGUAGE="JavaScript">";
echo "location.href=’$url’";
echo "</SCRIPT>-->";
?>
<?php
$url="http://localhost/";
echo "<!--<SCRIPT LANGUAGE="JavaScript">";
echo "location.href=’$url’";
echo "</SCRIPT>-->";
?>