Copy Code code as follows:
<?php
/*
* 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 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 SQL query, do not get and cache results of rows
function query_unbuffered ($sql = "") {
return $this->query ($sql, ' mysql_unbuffered_query ');
}
To 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, valid only 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 digital encoding of the error message in the previous MySQL operation
function Get_errno () {
$this->errno = @mysql_errno ($this->connection_id);
return $this->errno;
}
Get the ID generated by the previous INSERT operation
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);
}
Listing 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 the result set and return as an object, get all the 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 tips
Function Halt ($the _error= "") {
$message = $the _error. " <br/>\r\n ";
$message. = $this->get_errno (). "<br/>\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 "echo "<style type=\" text/css\ "><!--. error {font:11px tahoma, Verdana, Arial, Sans-serif, SimSun;} --></style>echo "<body>\r\n";
echo "<blockquote>\r\n";
echo "<textarea class=\" error\ "rows=\" 15\ "cols=\" 100\ "wrap=\" on\ ">". Htmlspecialchars ($message). "</textarea>\r\n";
echo "</blockquote>\r\n</body>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 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 based on criteria, return all records
* $tbname table name, $where query criteria, $limit return records, $fields return field
*/
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);
}
Display 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"];
}
}
?>
For a long time no post, I used to share some of the PHP class files.
If you have added new features, or have improved, please share with us.
Copy Code code as follows:
<?php
$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: All records of cid=1 in the query table table_name.
$row = $db->row_select (' table_name ', ' cid=1 ');
?>
For more detailed usage, refer to the comments in the DB class file.