資料庫操縱基本流程為:
1、串連資料庫伺服器
2、選擇資料庫
3、執行SQL語句
4、處理結果集
5、列印操作資訊
其中用到的相關函數有
•resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) 串連資料庫伺服器
•resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] ) 串連資料庫伺服器,長串連
•int mysql_affected_rows ( [resource link_identifier] )取得最近一次與 link_identifier 關聯的 INSERT,UPDATE 或 DELETE 查詢所影響的記錄行數。
•bool mysql_close ( [resource link_identifier] )如果成功則返回 TRUE,失敗則返回 FALSE。
•int mysql_errno ( [resource link_identifier] )返回上一個 MySQL 函數的錯誤號碼,如果沒有出錯則返回 0(零)。
•string mysql_error ( [resource link_identifier] )返回上一個 MySQL 函數的錯誤文本,如果沒有出錯則返回 ''(Null 字元串)。如果沒有指定串連資源號,則使用上一個成功開啟的串連從 MySQL 伺服器提取錯誤資訊。
•array mysql_fetch_array ( resource result [, int result_type] )返回根據從結果集取得的行產生的數組,如果沒有更多行則返回 FALSE。
•bool mysql_free_result ( resource result )釋放所有與結果標識符 result 所關聯的記憶體。
•int mysql_num_fields ( resource result )返回結果集中欄位的數目。
•int mysql_num_rows ( resource result )返回結果集中行的數目。此命令僅對 SELECT 語句有效。要取得被 INSERT,UPDATE 或者 DELETE 查詢所影響到的行的數目,用 mysql_affected_rows()。
•resource mysql_query ( string query [, resource link_identifier] ) 向與指定的串連標識符關聯的伺服器中的當前活動資料庫發送一條查詢。php如果沒有指定 link_identifier,則使用上一個開啟的串連。如果沒有開啟的串連,本函數會嘗試無參數調用 mysql_connect() 函數來建立一個串連並使用之。查詢結果會被緩衝
代碼如下:來源php教程中心
. 代碼如下:
class mysql {
private $db_host; //資料庫主機
private $db_user; //資料庫登陸名
private $db_pwd; //資料庫登陸密碼
private $db_name; //資料庫名
private $db_charset; //資料庫字元編碼
private $db_pconn; //長串連標識位
private $debug; //調試開啟
private $conn; //資料庫連接標識
private $msg = ""; //資料庫操縱資訊
// private $sql = ""; //待執行的SQL語句
public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_name = $db_name;
$this->db_charset = $db_chaeset;
$this->db_pconn = $db_pconn;
$this->result = '';
$this->debug = $debug;
$this->initConnect();
}
public function initConnect() {
if ($this->db_pconn) {
$this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
$this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}
if ($this->conn) {
$this->query("SET NAMES " . $this->db_charset);
} else {
$this->msg = "資料庫連接出錯,錯誤編號:" . mysql_errno() . "錯誤原因:" . mysql_error();
}
$this->selectDb($this->db_name);
}
public function selectDb($dbname) {
if ($dbname == "") {
$this->db_name = $dbname;
}
if (!mysql_select_db($this->db_name, $this->conn)) {
$this->msg = "資料庫不可用";
}
}
public function query($sql, $debug = false) {
if (!$debug) {
$this->result = @mysql_query($sql, $this->conn);
} else {
}
if ($this->result == false) {
$this->msg = "sql執行出錯,錯誤編號:" . mysql_errno() . "錯誤原因:" . mysql_error();
}
// var_dump($this->result);
}
public function select($tableName, $columnName = "*", $where = "") {
$sql = "SELECT " . $columnName . " FROM " . $tableName;
$sql .= $where ? " WHERE " . $where : null;
$this->query($sql);
}
public function findAll($tableName) {
$sql = "SELECT * FROM $tableName";
$this->query($sql);
}
public function insert($tableName, $column = array()) {
$columnName = "";
$columnValue = "";
foreach ($column as $key => $value) {
$columnName .= $key . ",";
$columnValue .= "'" . $value . "',";
}
$columnName = substr($columnName, 0, strlen($columnName) - 1);
$columnValue = substr($columnValue, 0, strlen($columnValue) - 1);
$sql = "INSERT INTO $tableName($columnName) VALUES($columnValue)";
$this->query($sql);
if($this->result){
$this->msg = "資料插入成功。新插入的id為:" . mysql_insert_id($this->conn);
}
}
public function update($tableName, $column = array(), $where = "") {
$updateValue = "";
foreach ($column as $key => $value) {
$updateValue .= $key . "='" . $value . "',";
}
$updateValue = substr($updateValue, 0, strlen($updateValue) - 1);
$sql = "UPDATE $tableName SET $updateValue";
$sql .= $where ? " WHERE $where" : null;
$this->query($sql);
if($this->result){
$this->msg = "資料更新成功。受影響行數:" . mysql_affected_rows($this->conn);
}
}
public function delete($tableName, $where = ""){
$sql = "DELETE FROM $tableName";
$sql .= $where ? " WHERE $where" : null;
$this->query($sql);
if($this->result){
$this->msg = "資料刪除成功。受影響行數:" . mysql_affected_rows($this->conn);
}
}
public function fetchArray($result_type = MYSQL_BOTH){
$resultArray = array();
$i = 0;
while($result = mysql_fetch_array($this->result, $result_type)){
$resultArray[$i] = $result;
$i++;
}
return $resultArray;
}
// public function fetchObject(){
// return mysql_fetch_object($this->result);
// }
public function printMessage(){
return $this->msg;
}
public function freeResult(){
@mysql_free_result($this->result);
}
public function __destruct() {
if(!empty($this->result)){
$this->freeResult();
}
mysql_close($this->conn);
}
}
調用代碼如下
. 代碼如下:
require_once 'mysql_V1.class.php';
require_once 'commonFun.php';
$db = new mysql('localhost', 'root', '', "test");
//select 查
$db->select("user", "*", "username = 'system'");
$result = $db->fetchArray(MYSQL_ASSOC);
print_r($result);
dump($db->printMessage());
//insert 增
//$userInfo = array('username'=>'system', 'password' => md5("system"));
//$db->insert("user", $userInfo);
//dump($db->printMessage());
//update 改
//$userInfo = array('password' => md5("123456"));
//$db->update("user", $userInfo, "id = 2");
//dump($db->printMessage());
//delete 刪
//$db->delete("user", "id = 1");
//dump($db->printMessage());
//findAll 查詢全部
$db->findAll("user");
$result = $db->fetchArray();
dump($result);
http://www.bkjia.com/PHPjc/477577.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477577.htmlTechArticle資料庫操縱基本流程為: 1、串連資料庫伺服器 2、選擇資料庫 3、執行SQL語句 4、處理結果集 5、列印操作資訊 其中用到的相關函數有 res...