PHP Connection MySQL database operation class This is a relatively full MySQL operation class Oh, yesterday wrote a simple connection MySQL database code, relative to this, that is the simplest, this is a data query, update, delete, and other operations.
PHP Tutorial Connect MySQL Tutorial Database tutorial Operation class
This is a relatively full MySQL operation class Oh, yesterday wrote a simple connection MySQL database code, relative to this, that is the simplest, this is a data query, update, delete, and other operations.
*/
Class mysql{
Private $db _host; Database Host
Private $db _user; Database user Name
Private $db _pwd; Database Password
Private $db _database; Database name
Private $conn; database connection identification;
Private $sql; Statements executed by SQL
Private $result; Resource identifier for query
Private $coding; Database encoding, gbk,utf8,gb2312
Private $show _error = true; Local debug use, print error
/**
* Constructor function
*
* @access Public
* @parameter string $db _host database Host
* @parameter string $db _user database user name
* @parameter string $db _pwd database Password
* @parameter string $db _database database name
* @parameter String $coding encoding
* @return void
*/
Public function __construct ($db _host, $db _user, $db _pwd, $db _database, $coding) {
$this->db_host = $db _host;
$this->db_user = $db _user;
$this->db_pwd = $db _pwd;
$this->db_database = $db _database;
$this->coding = $coding;
$this->connect ();
}
/**
* Link Database
*
* @access Private
* @return void
*/
Private Function Connect () {
$this->conn = @mysql_connect ($this->db_host, $this->db_user, $this->db_pwd);
if (! $this->conn) {
Print error when the Show_error is turned on
if ($this->show_error) {
$this->show_error (' ERROR hint: Link database failed! ');
}
}
if (! @mysql_select_db ($this->db_database, $this->conn)) {
Failed to open database
if ($this->show_error) {
$this->show_error (' ERROR hint: Open database failed! ');
}
}
if (! @mysql_query ("Set names $this->coding")) {
Failed to set encoding
if ($this->show_error) {
$this->show_error (' ERROR warning: setting encoding failed! ');
}
}
}
/**
* Executable query Add modify delete and so on any SQL statement
*
* @access Public
* @parameter string $sql SQL statement
* @return Resource Resource Identifier
*/
Public Function Query ($sql) {
$this->sql = $sql;
$result = mysql_query ($this->sql, $this->conn);
if (! $result) {
Query execution failed with print error
$this->show_error ("Wrong SQL statement:", $this->sql);
}else{
Returns the resource identifier
return $this->result = $result;
}
}
/**
* Query all databases in MySQL server
*
* @access Public
* @return void
*/
Public Function show_databases () {
$this->query ("Show Databases");
Total number of print databases
echo "Existing database:". Mysql_num_rows ($this->result);
echo "
";
$i = 1;
Loop out the name of each database
while ($row =mysql_fetch_array ($this->result)) {
echo "$i $row [Database]". "
";
$i + +;
}
}
/**
* Query the database for all table names
*
* @access Public
* @return void
*/
Public Function Show_tables () {
$this->query ("Show Tables");
Total number of printed tables
echo "Database {$this->db_database} is common." Mysql_num_rows ($this->result). "Zhang table:";
echo "
";
Constructs an array subscript, loops out all the table names of the database
$column _name = "Tables_in_". $this->db_database;
$i = 1;
Loop out the name of each table
while ($row =mysql_fetch_array ($this->result)) {
echo "$i $row [$column _name]". "
";
$i + +;
}
}
/**
* Take a Recordset, get an array-index and association
*
* @access Public
* @return void
*/
Public Function Fetch_array () {
Return mysql_fetch_array ($this->result);
}
/**
* Simplified SELECT query statement
*
* @access Public
* @parameter string $table table name
* @parameter string $field field name
* @return Resource
*/
Public function FindAll ($table, $field = ' * ') {
return $this->query ("Select $field from $table");
}
/**
* Simplified Delete query statement
*
* @access Public
* @parameter string $table table name
* @parameter string $condition The condition of the query
* @return Resource
*/
Public Function Delete ($table, $condition) {
return $this->query ("Delete from $table where $condition");
}
/**
* Simplified Insert INSERT statement
*
* @access Public
* @parameter string $table table name
* @parameter string $field field name
* @parameter string $value Insert Value
* @return Resource
*/
Public Function Insert ($table, $field, $value) {
return $this->query ("INSERT into $table ($field) VALUES (' $value ')");
}
/**
* Simplified UPDATE INSERT statement
*
* @access Public
* @parameter string $table table name
* @parameter the contents of string $update _content update
* @parameter string $condition condition
* @return Resource
*/
Public Function Update ($table, $update _content, $condition) {
return $this->query ("Update $table set $update _content where $condition");
}
/**
* Get the ID generated by the insert operation in the previous step
*
* @access Public
* @return Integer
*/
Public Function insert_id () {
return mysql_insert_id ();
}
/**
* Number of result set bars calculated
*
* @access Public
* @return Integer
*/
Public Function num_rows () {
Return mysql_num_rows ($this->result);
}
/**
* Query field number and field information
*
* @access Public
* @parameter string $table table name
* @return void
*/
Public Function Num_fields ($table) {
$this->query ("SELECT * from $table");
echo "
";
Number of print fields
echo "Number of fields:". $total = Mysql_num_fields ($this->result);
echo "
";
The Mysql_fetch_field () function obtains column information from the result set and returns it as an object.
for ($i = 0; $i < $total; $i + +) {
Print_r (Mysql_fetch_field ($this->result, $i));
}
echo "
";
echo "
";
}
/**
* Output SQL statement error message
*
* @access Public
* @parameter string $message tip information
* @return void
*/
Public Function show_error ($message = ', $sql = ') {
echo "
";
echo " error message Tip:
";
echo "";
Print Error Reason
echo "Error Reason:". Mysql_error (). "
";
Print error message
The Mysql_error () function returns the text error message generated by the previous MySQL operation.
echo "";
echo "". $message. "";
echo "";
Print Error SQL statement
echo "
" . $sql. "
";
echo "";
echo " ";
}
}
How to use
$mysql = new MySQL ($dbhost, $dbuser, $dbpwd, $dbname, $coding);
?>
http://www.bkjia.com/PHPjc/630818.html www.bkjia.com true http://www.bkjia.com/PHPjc/630818.html techarticle PHP Connection MySQL database operation class This is a relatively full MySQL operation class Oh, yesterday wrote a simple connection MySQL database code, relative to this, that is the simplest, ...