PHP Mysql full database connection class _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
Tags mysql host php mysql
Phpmysql full database connection class. Php Tutorial mysql tutorial complete database tutorial connection class * classmysql {private $ db_host; database host private $ db_user; database username private $ db_pwd; database username password

Php Tutorial mysql tutorial complete database tutorial connection class
*/
Class mysql {
Private $ db_host; // database host
Private $ db_user; // database username
Private $ db_pwd; // database username and password
Private $ db_database; // database name
Private $ conn; // Database Connection ID;
Private $ result; // result resource ID for executing the query command
Private $ SQL; // SQL execution statement
Private $ row; // number of returned entries
Private $ coding; // database encoding, gbk, utf8, gb2312
Private $ bulletin = true; // whether to enable error records
Private $ show_error = false; // in the test phase, all errors are displayed, which has security risks and is disabled by default.
Private $ is_error = false; // determines whether an error is terminated immediately. the default value is true. we recommend that you do not enable the error because it is annoying to see nothing when there is a problem.

/* Constructor */
Public function _ construct ($ db_host, $ db_user, $ db_pwd, $ db_database, $ conn, $ coding ){
$ This-> db_host = $ db_host;
$ This-> db_user = $ db_user;
$ This-> db_pwd = $ db_pwd;
$ This-> db_database = $ db_database;
$ This-> conn = $ conn;
$ This-> coding = $ coding;
$ This-> connect ();
}

/* Database connection */
Public function connect (){
If ($ this-> conn = "pconn "){
// Permanent link
$ This-> conn = mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
} Else {
// Even if the link
$ This-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
}

If (! Mysql_select_db ($ this-> db_database, $ this-> conn )){
If ($ this-> show_error ){
$ This-> show_error ("database unavailable:", $ this-> db_database );
}
}
Mysql_query ("set names $ this-> coding ");
}

/* Database execution statements: execute queries, add, modify, delete, and other SQL statements */
Public function query ($ SQL ){
If ($ SQL = ""){
$ This-> show_error ("SQL statement error:", "SQL query statement is empty ");
}
$ This-> SQL = $ SQL;

$ Result = mysql_query ($ this-> SQL, $ this-> conn );

If (! $ Result ){
// Used during debugging. the SQL statement is automatically printed when an error occurs.
If ($ this-> show_error ){
$ This-> show_error ("incorrect SQL statement:", $ this-> SQL );
}
} Else {
$ This-> result = $ result;
}
Return $ this-> result;
}

/* Create and add a new database */
Public function create_database ($ database_name ){
$ Database = $ database_name;
$ Sqldatabase = 'create database'. $ database;
$ This-> query ($ sqldatabase );
}

/* Query all databases on the server */
// Separate the system database from the user database for more intuitive display?
Public function show_databases (){
$ This-> query ("show databases ");
Echo "existing database:". $ amount = $ this-> db_num_rows ($ rs );
Echo"
";
$ I = 1;
While ($ row = $ this-> fetch_array ($ rs )){
Echo "$ I $ row [database]";
Echo"
";
$ I ++;
}
}

// Returns the names of all databases on the host in an array.
Public function databases (){
$ Rsptr = mysql_list_dbs ($ this-> conn );
$ I = 0;
$ Cnt = mysql_num_rows ($ rsptr );
While ($ I <$ cnt ){
$ Rs [] = mysql_db_name ($ rsptr, $ I );
$ I ++;
}
Return $ rs;
}

/* Query all tables in the database */
Public function show_tables ($ database_name ){
$ This-> query ("show tables ");
Echo "existing database:". $ amount = $ this-> db_num_rows ($ rs );
Echo"
";
$ I = 1;
While ($ row = $ this-> fetch_array ($ rs )){
$ Columnname = "tables_in _". $ database_name;
Echo "$ I $ row [$ columnname]";
Echo"
";
$ I ++;
}
}

/*
Mysql_fetch_row () array $ row [0], $ row [1], $ row [2]
Mysql_fetch_array () array $ row [0] or $ row [id]
Mysql_fetch_assoc () array is case sensitive with the $ row-> content field
Mysql_fetch_object () object is case sensitive with $ row [id] and $ row [content] Fields
*/

/* Obtain result data */
Public function mysql_result_li (){
Return mysql_result ($ str );
}

/* Obtain the record set and array-index and association. use $ row ['content'] */
Public function fetch_array ($ resultt = ""){
If ($ resultt <> ""){
Return mysql_fetch_array ($ resultt );
} Else {
Return mysql_fetch_array ($ this-> result );
}
}

// Obtain the joined array, using $ row ['Field name']
Public function fetch_assoc (){
Return mysql_fetch_assoc ($ this-> result );
}

// Obtain the numeric index array, using $ row [0], $ row [1], $ row [2]
Public function fetch_row (){
Return mysql_fetch_row ($ this-> result );
}

// Obtain the object array using $ row-> content
Public function fetch_object (){
Return mysql_fetch_object ($ this-> result );
}

// Simplified query select
Public function findall ($ table ){
$ This-> query ("select * from $ table ");
}

// Simplified query select
Public function select ($ table, $ columnname = "*", $ condition = '', $ debug = ''){
$ Condition = $ condition? 'Where'. $ condition: null;
If ($ debug ){
Echo "select $ columnname from $ table $ condition ";
} Else {
$ This-> query ("select $ columnname from $ table $ condition ");
}
}

// Simplify del deletion
Public function delete ($ table, $ condition, $ url = ''){
If ($ this-> query ("delete from $ table where $ condition ")){
If (! Empty ($ url ))
$ This-> get_admin_msg ($ url, 'deleted successfully! ');
}
}

// Simplify insert
Public function insert ($ table, $ columnname, $ value, $ url = ''){
If ($ this-> query ("insert into $ table ($ columnname) values ($ value )")){
If (! Empty ($ url ))
$ This-> get_admin_msg ($ url, 'added successfully! ');
}
}

// Simplify update modification
Public function update ($ table, $ mod_content, $ condition, $ url = ''){
// Echo "update $ table set $ mod_content where $ condition"; exit ();
If ($ this-> query ("update $ table set $ mod_content where $ condition ")){
If (! Empty ($ url ))
$ This-> get_admin_msg ($ url );
}
}

/* Obtain the id generated by the previous insert operation */
Public function insert_id (){
Return mysql_insert_id ();
}

// Point to a specific data record
Public function db_data_seek ($ id ){
If ($ id> 0 ){
$ Id = $ id-1;
}
If (! @ Mysql_data_seek ($ this-> result, $ id )){
$ This-> show_error ("SQL statement error:", "the specified data is empty ");
}
Return $ this-> result;
}

// Calculate the number of result sets based on the select query results
Public function db_num_rows (){
If ($ this-> result = null ){
If ($ this-> show_error ){
$ This-> show_error ("SQL statement error", "temporarily empty, no content! ");
}
} Else {
Return mysql_num_rows ($ this-> result );
}
}

// Obtain the number of affected rows based on insert, update, and delete execution results
Public function db_affected_rows (){
Return mysql_affected_rows ();
}

// Output the SQL statement
Public function show_error ($ message = "", $ SQL = ""){
If (! $ SQL ){
Echo "". $ message ."";
Echo"
";
} Else {
Echo"

";
Echo" Error message:
";
Echo"

";
Echo"

";
Echo "error code: 12142 ";
Echo"


";
Echo "error cause:". mysql_error ()."

";
Echo"

";
Echo "". $ message ."";
Echo"

";
Echo"
" . $sql . "
";
$ Ip = $ this-> getip ();
If ($ this-> bulletin ){
$ Time = date ("y-m-d h: I: s ");
$ Message = $ message. "rn $ this-> SQL". "rn client ip: $ ip". "rn time: $ time". "rnrn ";

$ Server_date = date ("y-m-d ");
$ Filename = $ server_date. ". txt ";
$ File_path = "error/". $ filename;
$ Error_content = $ message;
// $ Error_content = "wrong database, cannot be connected ";
$ File = "error"; // sets the file storage directory.

// Create a folder
If (! File_exists ($ file )){
If (! Mkdir ($ file, 0777 )){
// The default mode is 0777, which means the maximum possible access.
Die ("upload files directory does not exist and creation failed ");
}
}

// Create a txt date file
If (! File_exists ($ file_path )){

// Echo "create date file ";
Fopen ($ file_path, "w + ");

// First, make sure that the file exists and is writable.
If (is_writable ($ file_path )){
// Open $ filename in add mode. the file pointer will start with the file
If (! $ Handle = fopen ($ file_path, 'A ')){
Echo "the file $ filename cannot be opened ";
Exit;
}

// Write $ somecontent to the open file.
If (! Fwrite ($ handle, $ error_content )){
Echo "cannot be written to file $ filename ";
Exit;
}

// Echo "file $ filename written successfully ";

Echo "-- the error record is saved! ";

// Close the file
Fclose ($ handle );
} Else {
Echo "file $ filename cannot be written ";
}

} Else {
// First, make sure that the file exists and is writable.
If (is_writable ($ file_path )){
// Open $ filename in add mode. the file pointer will start with the file
If (! $ Handle = fopen ($ file_path, 'A ')){
Echo "the file $ filename cannot be opened ";
Exit;
}

// Write $ somecontent to the open file.
If (! Fwrite ($ handle, $ error_content )){
Echo "cannot be written to file $ filename ";
Exit;
}

// Echo "file $ filename written successfully ";
Echo "-- the error record is saved! ";

// Close the file
Fclose ($ handle );
} Else {
Echo "file $ filename cannot be written ";
}
}

}
Echo"
";
If ($ this-> is_error ){
Exit;
}
}
Echo"

";
Echo" ";

Echo"
";
}

// Release the result set
Public function free (){
@ Mysql_free_result ($ this-> result );
}

// Database selection
Public function select_db ($ db_database ){
Return mysql_select_db ($ db_database );
}

// Query the number of fields
Public function num_fields ($ table_name ){
// Return mysql_num_fields ($ this-> result );
$ This-> query ("select * from $ table_name ");
Echo"
";
Echo "number of fields:". $ total = mysql_num_fields ($ this-> result );
Echo"

";
for ($i = 0; $i < $total; $i++) {
print_r(mysql_fetch_field($this->result, $i));
}
echo "
";
Echo"
";
}

// Obtain mysql server information
Public function mysql_server ($ num = ''){
Switch ($ num ){
Case 1:
Return mysql_get_server_info (); // mysql server information
Break;

Case 2:
Return mysql_get_host_info (); // Obtain mysql host information
Break;

Case 3:
Return mysql_get_client_info (); // Obtain mysql client information
Break;

Case 4:
Return mysql_get_proto_info (); // Obtain mysql protocol information
Break;

Default:
Return mysql_get_client_info (); // Obtain the mysql version by default.
}
}

// Destructor, which automatically closes the database and recycles garbage
Public function _ destruct (){
If (! Empty ($ this-> result )){
$ This-> free ();
}
Mysql_close ($ this-> conn );
} // Function _ destruct ();

/* Obtain the real IP address of the client */
Function getip (){
If (getenv ("http_client_ip") & strcasecmp (getenv ("http_client_ip"), "unknown ")){
$ Ip = getenv ("http_client_ip ");
} Else
If (getenv ("http_x_forwarded_for") & strcasecmp (getenv ("http_x_forwarded_for"), "unknown ")){
$ Ip = getenv ("http_x_forwarded_for ");
} Else
If (getenv ("remote_addr") & strcasecmp (getenv ("remote_addr"), "unknown ")){
$ Ip = getenv ("remote_addr ");
} Else
If (isset ($ _ server ['remote _ addr ']) & $ _ server ['remote _ addr '] & strcasecmp ($ _ server ['remote _ addr'], "unknown ")){
$ Ip = $ _ server ['remote _ addr '];
} Else {
$ Ip = "unknown ";
}
Return ($ ip );
}
Function inject_check ($ SQL _str) {// prevents injection
$ Check = eregi ('select | insert | update | delete | '|/* | .. /|. /| union | into | load_file | outfile ', $ SQL _str );
If ($ check ){
Echo "illegal injection content entered! ";
Exit ();
} Else {
Return $ SQL _str;
}
}
Function checkurl () {// check the origin path
If (preg_replace ("/https tutorial? : // ([^:/] +). */I "," 1 ", $ _ server ['http _ referer'])! = Preg_replace ("/([^:] +). */", "1", $ _ server ['http _ host']) {
Header ("location: http://www.zhutiai.com ");
Exit ();
}
}

}

Complete mysql tutorial full database tutorial connection class */class mysql {private $ db_host; // database host private $ db_user; // database username private $ db_pwd; // password of the database username...

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.