The basic process of database manipulation is:
1. Connect to the database server
2. Select database
3. Execute SQL statement
4. Process the result set
5. Print operation information
The related functions used are
•resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) Connect to the database server
•resource mysql_pconnect ( [string server [, string username [ , string password [, int client_flags]]]] ) Connect to the database server, long connection
•int mysql_affected_rows ( [resource link_identifier] ) Get the number of record rows affected by the latest INSERT, UPDATE or DELETE query associated with link_identifier.
•bool mysql_close ( [resource link_identifier] ) Returns TRUE if successful, FALSE if failed.
•int mysql_errno ( [resource link_identifier] ) returns the error number of the previous MySQL function, or 0 (zero) if there is no error.
•string mysql_error ([resource link_identifier]) Returns the error text of the previous MySQL function, or '' (empty string) if there is no error. If no connection resource number is specified, the last successfully opened connection is used to extract error information from the MySQL server.
•array mysql_fetch_array ( resource result [, int result_type] ) Returns an array based on the rows fetched from the result set, or FALSE if there are no more rows.
•bool mysql_free_result (resource result) Releases all memory associated with the result identifier result.
•int mysql_num_fields (resource result) returns the number of fields in the result set.
•int mysql_num_rows (resource result) returns the number of rows in the result set. This command is only valid for SELECT statements. To get the number of rows affected by an INSERT, UPDATE, or DELETE query, use mysql_affected_rows().
• resource mysql_query ( string query [, resource link_identifier] ) Sends a query to the currently active database in the server associated with the specified connection identifier. If link_identifier is not specified, the last opened connection is used. If there is no open connection, this function will try to call the mysql_connect() function without parameters to establish a connection and use it. The query results will be cached
The code is as follows:
Copy code The code is as follows:
class mysql {
private $db_host; //Database host
private $db_user; //Database login name
private $db_pwd; //Database login password
private $db_name; //Database name
private $db_charset; //Database character encoding
private $db_pconn; //Long connection flag
private $debug; //Debugging is enabled
private $conn ; ; 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-> 🎜 > $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);
$ this-& gt; msg = "Database connect to error, error number:". Mysql_errno (). "Error reason:". Mysql_error (); ->db_name);
}
public function selectDb($dbname) {
if ($dbname == "") {
} $this->db_name = $dbname;
} if (! MySQL_SELECT_DB ($ this-& gt; db_name, $ this- & gt; conn)) {
$ this- & gt; msg = "Database cannot be available";
} }
public function query($sql, $debug = false) {
if (!$debug) {
result = @mysql_query($sql, $this- >conn);
} else {
}
} if ($this->result == false) {
} else Number: " . mysql_errno() . "Error reason: " . 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);
ps,个人比较喜欢tp的dump函数,所以在commonFun.php文件中拷贝了友好打印函数。使用时将其改为print_r()即可。
http://www.bkjia.com/PHPjc/326676.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326676.htmlTechArticle数据库操纵基本流程为: 1、连接数据库服务器 2、选择数据库 3、执行SQL语句 4、处理结果集 5、打印操作信息 其中用到的相关函数有 res...