Mysql database connection

Source: Internet
Author: User
Tags mysql connect php mysql php mysql connect

Mysql database connection
/*******************************
* PHP MySQL Connect
* Author: Ray
* Date: 2005/07/30
* Updata: 2005/10/23
* Version: 0.1
*/
 
/* Determine whether the required parameters for database connection exist */
If (empty ($ dbhost ))
{
$ Error = "<li> the Database Host Name is blank </li> ";
}
If (empty ($ dbuser ))
{
$ Error. = "<li> the database username is blank </li> ";
}
/* If (empty ($ dbpass ))
{
$ Error. = "<li> the Database Password is blank </li> ";
}*/
If (empty ($ dbname ))
{
$ Error. = "<li> the database name is blank </li> ";
}

If (isset ($ error) showErrorMessage ("1", "database operation failed", $ error );

/* Connect to the database */
If (! $ Connect = @ mysql_connect ($ dbhost, $ dbuser, $ dbpass ))
{
$ Error = "<li> unable to connect to the Database Host </li> <li> MySQL error message:". mysql_error (). "</li> ";
ShowErrorMessage ("1", "database connection failed", $ error); exit;
}

If (! $ Select_db = @ mysql_select_db ($ dbname ))
{
$ Error = "<li> unable to open database </li> <li> MySQL error message:". mysql_error (). "</li> ";
ShowErrorMessage ("1", "failed to open database", $ error); exit;
}
Mysql_query ("set names 'gbk '");
/* Database query */
Function dbQuery ($ mysql_query, $ debug = 0)
{
If ($ debug! = 0)
{
Echo $ mysql_query. "n ";
If ($ debug = 1) return;
}
$ GLOBALS ['mysql'] ['query _ count'] ++;
If (! $ Query = @ mysql_query ($ mysql_query ))
{
$ Error = "<li> MySQL statement encountered some errors during execution </li> <li> MySQL statement :". $ mysql_query. "</li> <li> MySQL error message :". mysql_error (). "</li> ";
ShowErrorMessage ("1", "invalid MySql query", $ error); exit;
}
If ($ debug! = 0) echo "ROWS:". mysql_num_rows ($ query );
Return $ query;
}

/* The query results are decomposed into arrays */
Function dbArray ($ mysql_result)
{
Return @ mysql_fetch_array ($ mysql_result );
}

Function queryBot ($ query_data)
{
If (! Empty ($ query_data ['field'])
{
Foreach ($ query_data ['field'] as $ field => $ value)
{
Switch ($ query_data ['action'])
{
Case 'insert ':
$ Query_insert_field [(int) $ I]. = "'". $ field ."'";
$ Query_insert_value [(int) $ I]. = "'". $ value ."'";
Break;
Case 'update ':
$ Query_update_field [(int) $ I]. = "'". $ field. "' = '". $ value ."'";
Unset ($ query_update_where );
Foreach ($ query_data ['where'] as $ field => $ value)
{
$ Query_update_where [(int) $ j]. = "'". $ field. "' = '". $ value ."'";
}
$ J ++;
Break;
}
$ I ++;
}
Switch ($ query_data ['action'])
{
Case 'insert ':
DbQuery ("insert '". $ GLOBALS ['prefix']. $ query_data ['table']. "'(". implode (",", $ query_insert_field ). ") VALUES (". implode (",", $ query_insert_value ). ");");
Break;
Case 'update ':
DbQuery ("Update '". $ GLOBALS ['prefix']. $ query_data ['table']. "'set ". implode (",", $ query_update_field ). "WHERE ". implode ("AND", $ query_update_where ). ";");
Break;
}
}
}

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.