PHP MySQL database operation class _php tutorial

Source: Internet
Author: User
Tags pconnect php mysql
Copy CodeThe code is as follows:
/*
* MySQL Database DB class
* @package DB
* @author yytcpt (no shadow)
* @version 2008-03-27
* @copyrigth http://www.d5s.cn/
*/
Class DB {
var $connection _id = "";
var $pconnect = 0;
var $shutdown _queries = Array ();
var $queries = array ();
var $query _id = "";
var $query _count = 0;
var $record _row = Array ();
var $failed = 0;
var $halt = "";
var $query _log = Array ();
Function connect ($db _config) {
if ($this->pconnect) {
$this->connection_id = mysql_pconnect ($db _config["hostname"], $db _config["username"], $db _config["password"]);
}else{
$this->connection_id = mysql_connect ($db _config["hostname"], $db _config["username"], $db _config["password"]);
}
if (! $this->connection_id) {
$this->halt ("Can not connect to MySQL Server");
}
if (! @mysql_select_db ($db _config["Database"], $this->connection_id)) {
$this->halt ("Can not connect MySQL Database");
}
if ($db _config["CharSet"]) {
@mysql_unbuffered_query ("SET NAMES". $db _config["CharSet"]. "'");
}
return true;
}
Send the SQL query and return the result set
function query ($query _id, $query _type= ' mysql_query ') {
$this->query_id = $query _type ($query _id, $this->connection_id);
$this->queries[] = $query _id;
if (! $this->query_id) {
$this->halt ("Query failed: \n$query_id");
}
$this->query_count++;
$this->query_log[] = $str;
return $this->query_id;
}
Send a SQL query that does not fetch and cache the resulting rows
function query_unbuffered ($sql = "") {
return $this->query ($sql, ' mysql_unbuffered_query ');
}
Get a row from the result set as an associative array
function Fetch_array ($sql = "") {
if ($sql = = "") $sql = $this->query_id;
$this->record_row = @mysql_fetch_array ($sql, MYSQL_ASSOC);
return $this->record_row;
}
function Shutdown_query ($query _id = "") {
$this->shutdown_queries[] = $query _id;
}
Gets the number of rows in the result set, only for Insert,update or DELETE
function Affected_rows () {
Return @mysql_affected_rows ($this->connection_id);
}
Gets the number of rows in the result set, only valid for the SELECT statement
function num_rows ($query _id= "") {
if ($query _id = = "") $query _id = $this->query_id;
Return @mysql_num_rows ($query _id);
}
Returns the numeric encoding of the error message in the previous MySQL operation
function Get_errno () {
$this->errno = @mysql_errno ($this->connection_id);
return $this->errno;
}
Gets the ID generated by the INSERT operation in the previous step
function insert_id () {
Return @mysql_insert_id ($this->connection_id);
}
Number of queries received
function Query_count () {
return $this->query_count;
}
Releasing the resulting memory
function Free_result ($query _id= "") {
if ($query _id = = "") $query _id = $this->query_id;
@mysql_free_result ($query _id);
}
Close MySQL Connection
function close_db () {
if ($this->connection_id) return @mysql_close ($this->connection_id);
}
List the tables in the MySQL database
function Get_table_names () {
Global $db _config;
$result = Mysql_list_tables ($db _config["Database"]);
$num _tables = @mysql_numrows ($result);
for ($i = 0; $i < $num _tables; $i + +) {
$tables [] = Mysql_tablename ($result, $i);
}
Mysql_free_result ($result);
return $tables;
}
Get column information from result set and return as Object, get all fields
function Get_result_fields ($query _id= "") {
if ($query _id = = "") $query _id = $this->query_id;
while ($field = Mysql_fetch_field ($query _id)) {
$fields [] = $field;
}
return $fields;
}
Error hints
Function Halt ($the _error= "") {
$message = $the _error. "
\ r \ n ";
$message. = $this->get_errno (). "
\ r \ n ";
$sql = "INSERT into ' db_error ' (PAGENAME, errstr, timer) VALUES ('". $_server["Php_self"]. "', '". Addslashes ($message). "', ". Time ().") ";
@mysql_unbuffered_query ($sql);
if (debug==true) {
echo " <title>MySQL Database error</title>";
echo " \ r \ n ";
echo "\ r \ n ";
echo "
\ r \ n ";
echo " ". Htmlspecialchars ($message). "\ r \ n";
echo "
\ r \ n";
Exit
}
}
function __destruct () {
$this->shutdown_queries = Array ();
$this->close_db ();
}
function Sql_select ($tbname, $where = "", $limit =0, $fields = "*", $orderby = "id", $sort = "DESC") {
$sql = "Select". $fields. " From ' ". $tbname." ` ". ($where? " WHERE ". $where:" ")." ORDER by ". $orderby." ". $sort. ($limit? "Limit". $limit: "");
return $sql;
}
function Sql_insert ($tbname, $row) {
foreach ($row as $key = = $value) {
$sqlfield. = $key. ",";
$sqlvalue. = "'". $value. "',";
}
Return "INSERT into". $tbname. " ' (". substr ($sqlfield, 0,-1).") VALUES (". substr ($sqlvalue, 0,-1).") ";
}
function Sql_update ($tbname, $row, $where) {
foreach ($row as $key = = $value) {
$sqlud. = $key. " = ' ". $value." ', ";
}
Return "UPDATE". $tbname. " ' SET '. substr ($sqlud, 0,-1). " WHERE ". $where;
}
function Sql_delete ($tbname, $where) {
Return "DELETE from". $tbname. " ' WHERE '. $where;
}
Add a new record
function Row_insert ($tbname, $row) {
$sql = $this->sql_insert ($tbname, $row);
return $this->query_unbuffered ($sql);
}
Update a specified record
function Row_update ($tbname, $row, $where) {
$sql = $this->sql_update ($tbname, $row, $where);
return $this->query_unbuffered ($sql);
}
Delete a record that satisfies a condition
function Row_delete ($tbname, $where) {
$sql = $this->sql_delete ($tbname, $where);
return $this->query_unbuffered ($sql);
}
/* Query by criteria, return all records
* $tbname table name, $where query criteria, $limit return records, $fields return fields
*/
function Row_select ($tbname, $where = "", $limit =0, $fields = "*", $orderby = "id", $sort = "DESC") {
$sql = $this->sql_select ($tbname, $where, $limit, $fields, $orderby, $sort);
return $this->row_query ($sql);
}
Show a record in detail
function Row_select_one ($tbname, $where, $fields = "*", $orderby = "id") {
$sql = $this->sql_select ($tbname, $where, 1, $fields, $orderby);
return $this->row_query_one ($sql);
}
function Row_query ($sql) {
$rs = $this->query ($sql);
$rs _num = $this->num_rows ($rs);
$rows = Array ();
for ($i =0; $i < $rs _num; $i + +) {
$rows [] = $this->fetch_array ($rs);
}
$this->free_result ($RS);
return $rows;
}
function Row_query_one ($sql) {
$rs = $this->query ($sql);
$row = $this->fetch_array ($rs);
$this->free_result ($RS);
return $row;
}
Count statistics
function Row_count ($tbname, $where = "") {
$sql = "SELECT count (ID) as Row_sum from '". $tbname. " ` ". ($where? " WHERE ". $where:" ");
$row = $this->row_query_one ($sql);
return $row ["Row_sum"];
}
}
?>

A long time no post, I used to share some of the PHP class files.
If you add a new feature, or if there is an improvement, please share it with everyone.
Copy CodeThe code is as follows:
$db _config["hostname"] = "127.0.0.1"; Server address
$db _config["username"] = "root"; Database user Name
$db _config["password"] = "root"; Database Password
$DB _config["database"] = "wap_blueidea_com"; Database name
$db _config["charset"] = "UTF8";
Include (' db.php ');
$db = new db ();
$db->connect ($db _config);
Example: Querying all records cid=1 in the table table_name.
$row = $db->row_select (' table_name ', ' cid=1 ');
?>

Refer to the comments in the DB class file for more detailed usage.

http://www.bkjia.com/PHPjc/318985.html www.bkjia.com true http://www.bkjia.com/PHPjc/318985.html techarticle Copy the code as follows:? PHP/* *mysql DB Class * @packagedb * @authoryytcpt (no shadow) * @version2008 -03-27 * @copyrigthhttp://www.d5s.cn/* * classdb{var$connection_id= ""; var$p ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.