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 coding, gbk,utf8,gb2312
Private $show _error = true; Local debugging use, printing error
/**
* Constructors
*
* @access public
* @parame ter 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 Show_error is turned on
if ($this->show_error) {
$this->show_error (' ERROR tip: Link database failed! ');
}
}
if (! @mysql_select_db ($this->db_database, $this->conn)) {
Failed to open database
if ($this->show_error) {
$this->show_error (' Error prompt: Failed to open database! ');
}
}
if (! @mysql_query ("Set names $this->coding")) {
//setting encoding failed
if ($this->show_error) {
$this->show_error (' Error: Setting encoding failed! ');
}
}
}
/**
* Executable query add modify delete any SQL statements
*
* @access public
* @parameter string $sql sql statement
* @return resource resource Identifier
& nbsp */
public function Query ($sql) {
$this->sql = $sql;
$ result = mysql_query ($this->sql, $this->conn);
if (! $result) {
//query execution failed, printing error
$ This->show_error ("Bad SQL statement:", $this->sql);
}else{
//Returns the resource identifier
return $this-> result = $result;
}
}
/**
* Query all databases in the MySQL server
*
* @access public
& nbsp * @return void
*/
public function show_databases () {
$this-> Query ("show Databases");
//The total number of print databases
echo "Existing Databases:". Mysql_num_rows ($this->result);
echo "<br/>";
$i = 1;
//Cycle Output The name of each database
while ($row =mysql_fetch_array ($this->result)) {
echo "$i $row [Database]". "<br/>";
$i + +;
}
}
/**
* Query database 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} total. mysql_num_rows ($this- >result). "Sheet:";
echo "<br/>";
//constructs an array subscript, loops out all table names for the database
$column _name = "Tables_in_". $this->db_database ;
$i = 1;
//Cycle Output The name of each table
while ($row =mysql_fetch_array ($this->result)) {
echo "$i $row [$column _name]". "<br/>";
$i + +;
}
}
/**
* Get the Recordset, get the array-Index and association
*
* @access Public
* @return void
*/
Public Function Fetch_array () {
Return mysql_fetch_array ($this->result);
}
/**
* Simplify SELECT query statements
*
* @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 Insertion 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 ("Inserts into $table ($field) VALUES (' $value ')");
}
/**
* Simplify the UPDATE INSERT statement
*
* @access Public
* @parameter string $table table name
* @parameter string $update _content updated content
* @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 previous insert operation
*
* @access Public
* @return Integer
*/
Public Function insert_id () {
return mysql_insert_id ();
}
/**
* Calculate result set number
*
* @access public
* @ret Urn integer
*/
public function Num_rows () {
return mysql_num_ Rows ($this->result);
}
/**
* Query field quantity and field information
*
* @access Public
* @parameter string $table table name
* @return void
*/
Public Function Num_fields ($table) {
$this->query ("SELECT * from $table");
echo "<br/>";
Number of fields printed
echo number of fields:. $total = Mysql_num_fields ($this->result);
echo "<pre>";
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 "</pre>";
echo "<br/>";
}
/**
* Output SQL statement error message
*
* @access Public
* @parameter string $message hint information
* @return void
*/
Public Function show_error ($message = ', $sql = ') {
echo "<fieldset>";
echo "<legend> error message Tip: </legend><br/>";
echo "<div style= ' font-size:14px; Clear:both; Font-family:verdana, ARIAL, Helvetica, Sans-serif; > ";
Print Error Reason
echo "Error Reason:". Mysql_error (). "<br/><br/>";
Printing error messages
The Mysql_error () function returns the text error message generated by the previous MySQL operation.
echo "<div style= ' height:20px; Background: #ff0000; border:1px #ff0000 solid ' > ';
echo "<font color= ' White ' >". $message. "</font>";
echo "</div>";
Print Error SQL statement
echo "<font color= ' Red ' ><pre>". $sql. "</pre></font>";
echo "</div>";
echo "</fieldset>";
}
}
How to use
$mysql = new MySQL ($dbhost, $dbuser, $dbpwd, $dbname, $coding);
?>