Mysql Database Access Class _mysql

Source: Internet
Author: User
Tags first row
/**
* @Purpose: MySQL Database access class
* @Package:
* @Author: lisen@sellingclub.cn
* @Modifications:
* @See:
* @Time: 2008.10.10
*/
Class Db_mysql
{
//============================================================
Private $Host = ' localhost ';
Private $Database = ' db_name ';
Private $User = ' User ';
Private $Password = ' Password ';
//============================================================
Private $Link _id = 0; Database connection
Private $Query _id = 0; Query results
Private $Row _result = Array (); An array of result sets
Private $Field _result = Array (); Array of result set fields
Private $Affected _rows; Number of rows affected
Private $Rows; Number of rows recorded in the result set
Private $Fields; Number of fields in result set
Private $Row _postion = 0; Index of record pointer location
Public $Insert _id = 0;
//************************************************************
/**** Constructor ****/
function __construct ()
{
$this->connect ();
}
/**** destructor ****/
function __destruct ()
{
@mysql_free_result ($this->query_id);
Mysql_close ($this->link_id);
}
/**** connection server, select Database ****/
Function Connect ($Database = ', $Host = ', $User = ', $Password = ')
{
$Database = $Database = = ""? $this->database: $Database;
$Host = $Host = = ""? $this->host: $Host;
$User = $User = = ""? $this->user: $User;
$Password = $Password = = ""? $this->password: $Password;
//-----------------------------------------------------------//
if (0 = = $this->link_id)
{
$this->link_id = @mysql_pconnect ($Host, $User, $Password);
if (! $this->link_id)
{
$this->halt (' Connect database server failed! ');
}
if (!mysql_select_db ($this->database, $this->link_id))
{
$this->halt (' cannot open the specified database: '. $this->database);
}
}
return $this->link_id;
}
/**** Free Memory ****/
function free ()
{
if (@mysql_free_result ($this->query_id))
{
unset ($this->row_result);
}
$this->query_id = 0;
}
/**** Execute Query ****/
function query ($Query _string)
{
Free Last query memory
if ($this->query_id)
{
$this->free ();
}
if (0 = = $this->link_id)
{
$this->connect ();
}
Set Chinese Character set
@mysql_query (' Set names gb2312 ');
$this->query_id = mysql_query ($Query _string, $this->link_id);
$this->insert_id = mysql_insert_id ();
if (! $this->query_id)
{
$this->halt (' SQL query statement error: '. $Query _string);
}
@mysql_query (' Set names gb2312 ');
return $this->query_id;
}
/**** points The result set pointer to the specified row ****/
function Seek ($pos)
{
if (@mysql_data_seek ($this->query_id, $pos))
{
$this->row_position = $pos;
return true;
}
Else
{
$this->halt (' Locating result set has error! ');
return false;
}
}
/**** returns an array of result sets ****/
function Get_rows_array ()
{
$this->get_rows ();
for ($i = 0; $i < $this->rows; $i + +)
{
if (!mysql_data_seek ($this->query_id, $i))
{
$this->halt (' Mysql_data_seek query Error! ');
}
$this->row_result[$i] = mysql_fetch_array ($this->query_id);
}
return $this->row_result;
}
/**** returns an array of result set fields ****/
function Get_fields_array ()
{
$this->get_fields ();
for ($i = 0; $i < $this->fields; $i + +)
{
$obj = Mysql_fetch_field ($this->query_id, $i);
$this->field_result[$i] = $obj->name;
}
return $this->field_result;
}
/**** returns the number of impact records ****/
function Get_affected_rows ()
{
$this->affected_rows = mysql_affected_rows ($this->link_id);
return $this->affected_rows;
}
/**** returns the number of records in the result set ****/
function Get_rows ()
{
$this->rows = mysql_num_rows ($this->query_id);
return $this->rows;
}
/**** returns the number of fields in the result set ****/
function Get_fields ()
{
$this->fields = mysql_num_fields ($this->query_id);
return $this->fields;
}
/**** executes the SQL statement and returns an array of the first row of records in the query result ****/
function Fetch_one_array ($sql)
{@mysql_query (' Set names gb2312 ');
$this->query ($sql);
Return mysql_fetch_array ($this->query_id);
}
/**** Print error message ****/
function Halt ($msg)
{
$this->error = Mysql_error ();
printf ("<font style= ' font-family:arial, song body; font-size:12px; > <b> Database error:</b>%s \ n ", $msg);
printf ("MySQL returns error message:</b>%s \ n", $this->error);
printf ("error page: <font style= ' color: #0000EE; text-decoration:underline ' >%s</font> \ \", $_server[' php_self ') ]);
printf ("Please submit the error message to the system administrator or Web site programmer to handle it!") \ n ");
Die (' <b><font color=red> script termination </font></b></font> ');
}
}
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.