MySQL Database connection class

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

MySQL Database connection class
/*******************************
* PHP MySQL Connect
* Author:ray
* DATE:2005/07/30
* UPDATA:2005/10/23
* version:0.1
*/

/* Determine if the parameters required to connect to the database exist.
if (empty ($dbhost))
{
$error = "<li> database host name is null </li>";
}
if (empty ($dbuser))
{
$error. = "<li> database user name is null </li>";
}
/*if (Empty ($dbpass))
{
$error. = "<li> database password is null </li>";
}*/
if (empty ($dbname))
{
$error. = "<li> database name is null </li>";
}

if (Isset ($error)) showerrormessage ("1", "Database operation failed", $error);

/* Connection Database * *
if (! $connect = @mysql_connect ($dbhost, $dbuser, $dbpass))
{
$error = "<li> cannot connect to database host </li><li>mysql error message:". Mysql_error (). " </li> ";
Showerrormessage ("1", "Database connection Failed", $error);
}

if (! $select _db = @mysql_select_db ($dbname))
{
$error = "<li> Cannot open database </li><li>mysql error message:". Mysql_error (). </li> ";
Showerrormessage ("1", "Database open Failed", $error);
}
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 error </li><li>mysql statement at execution:". $mysql _query. " </li><li>mysql error message: ". Mysql_error ()." </li> ";
Showerrormessage ("1", "wrong MySQL query", $error); exit;
}
if ($debug!= 0) echo "ROWS:". mysql_num_rows ($query);
return $query;
}

/* 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 into". $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.