Useless for a long time -- PHP -- database connection class

Source: Internet
Author: User

<
?
PHP

/*
File Name: database. php
Role: Database
Author: mr. bool
Created at: 2007.10.22
Modification time: 2007.10.23
*/

Require_once
(
"Config. Inc. php"
)
;
// System Configuration File


/*
$ Dbhost = "localhost"; // host name
$ Dbuser = "root"; // database username
$ Dbpwd = ""; // Password
$ Dbname = "test"; // Database Name
*/

// Define the Database Class


Class
Database
{

// Define attributes


VaR
$
Mconnid
;
// Connection ID


VaR
$
Msqlstring
;
// SQL statement to be executed


VaR
$
Mresultarray
;
// An array of results returned by executing the SELECT statement



// _ Construct (), constructor to establish a database connection


Function
_ Construct (
$
Phost
,
$
Puser
,
$
Ppwd
,
$
Pdbname
)
{

$
This
-
>
Mconnid =
Mysql_connect
(
$
Phost
,
$
Puser
,
$
Ppwd
)
;
// Establish a connection


Mysql_select_db
(
$
Pdbname
,
$
This
-
>
Mconnid)
;
// Select a database


Mysql_query
(
"Set names 'gbk '"
)
;
// Set the database encoding to GBK


}


// _ Destruct: destructor, disconnected


Function
_ Destruct (
)
{

Mysql_close
(
$
This
-
>
Mconnid)
;
// There is still a problem here ......


}


// Execute the SQL statement


Function
Executesql (
)
{

Mysql_query
(
$
This
-
>
Msqlstring)
;

}


// Query data. The returned value is an array of objects. Each element in the array is an object composed of a row of records.


Function
Query (
)
{

$
I
=
0;

$
Query_result
=
Mysql_query
(
$
This
-
>
Msqlstring,
$
This
-
>
Mconnid)
;

While
(
$
Row
=
Mysql_fetch_object
(
$
Query_result
)
)
{

$
This
-
>
Mresultarray [
$
I
+
+
]
=
$
Row
;

}

}


}
// Class database

// Use the following for testing:


$
DB
=
New
Database (
$
Dbhost
,
$
Dbuser
,
$
Dbpwd
,
$
Dbname
)
;

$
DB
-
>
Msqlstring =
"Update student set phone = '000000' where id = '000000 '"
;

$
DB
-
>
Executesql (
)
;

$
DB
-
>
Msqlstring =
"Select * from student where id = '000000 '"
;

$
DB
-
>
Query (
)
;

Print_r
(
$
DB
-
>
Mresultarray)
;
// Output the test result



?
>

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.