php connect mysql database operation class

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags access class code coding connect data database password delete


This is a relatively full mysql operation Oh, yesterday wrote a simple link mysql database code, relative to this, that is the easiest, 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 ID;
private $ sql; / / sql statement executed
private $ result; // query resource identifier
private $ coding; / / Database coding, gbk, utf8, gb2312
private $ show_error = true; / / Debugging local use, printing error

/ **
* Constructor
*
* @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
* @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) {
// show_error is on, printing error
if ($ this-> show_error) {
$ this-> show_error ('error message: link database failed!');
}
}

if (! @ mysql_select_db ($ this-> db_database, $ this-> conn)) {
// failed to open the database
if ($ this-> show_error) {
$ this-> show_error ('error message: open the database failed!');
}
}

if (! @ mysql_query ("set names $ this-> coding")) {
// set the encoding failed
if ($ this-> show_error) {
$ this-> show_error ('error message: set the encoding failed!');
}
}
}

/ **
* Executable query to add modify delete 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, printing error
$ this-> show_error ("Wrong sql statement:", $ this-> sql);
} else {
// return resource identifier
return $ this-> result = $ result;
}
}

/ **
* Query all the databases in the mysql server
*
* @access public
* @return void
* /
public function show_databases () {
$ this-> query ("show databases");
// Print the total number of databases
echo "Existing database:". mysql_num_rows ($ this-> result);
echo "<br />";
$ i = 1;
// Loop out the name of each database
while ($ row = mysql_fetch_array ($ this-> result)) {
echo "$ i $ row [database]". "<br />";
$ i ++;
}
}

/ **
* Query all the table name under the database
*
* @access public
* @return void
* /
public function show_tables () {
$ this-> query ("show tables");
// Print the total number of tables
echo "Database {$ this-> db_database}" mysql_num_rows ($ this-> result). "Sheet:";
echo "<br />";
// Construct an array index, loop out all the database table name
$ 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]". "<br />";
$ i ++;
}
}

/ **
Get recordsets, get array - index and correlation
*
* @access public
* @return void
* /
public function fetch_array () {
return mysql_fetch_array ($ this-> result);
}

/ **
* Simplify select query
*
* @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
*
* @access public
* @parameter string $ table table name
* @parameter string $ condition Query conditions
* @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')");
}

/ **
* 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 the result set number
*
* @access public
* @return integer
* /
public function num_rows () {
return mysql_num_rows ($ this-> result);
}

/ **
* Query the number of fields and field information
*
* @access public
* @parameter string $ table table name
* @return void
* /
public function num_fields ($ table) {
$ this-> query ("select * from $ table");
echo "<br />";
// print the number of fields
echo "Number of fields:". $ total = mysql_num_fields ($ this-> result);
echo "<pre>";
// The mysql_fetch_field () function takes the 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 message
* @return void
* /
public function show_error ($ message = '', $ sql = '') {
echo "<fieldset>";
echo "<legend> error message: </ legend> <br />";
echo "<div style = 'font-size: 14px; clear: both; font-family: verdana, arial, helvetica, sans-serif;'>";
// Cause of printing error
echo "Cause of the error:". mysql_error (). "<br /> <br />";
// print error message
// mysql_error () function returns the text of a mysql operation error message.
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>";
}
}
//Instructions

$ mysql = new mysql ($ dbhost, $ dbuser, $ dbpwd, $ dbname, $ coding);
?>

Related Article

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.