Encapsulation of database operations in PHP

Source: Internet
Author: User
Packaging | data | database

We now encapsulate it in Dbfz.inc, which is designed as follows:
$#@60;?
Class dbinterface{var $dbID = 1;//To determine the current operation of the database, when DbID 1 for MySQL, when 2 for SQL Server, 3 for ODBC or other.
var $dbHost; Host domain name where the database resides
var $dbUsername; Database user Name
var $dbPassword; User password
Set host, username, and password functions
function Setparameter ($host, $username, $password) {
$this-$#@62;dbusername= $username;
$this-$#@62;dbhost= $host;
$this-$#@62;dbpassword= $password;
}
Join Number Library functions
function Dbconnect () {
Switch ($this-$#@62;dbid)
{
Case 1;
Return @mysql_connect ($this-$#@62;dbhost, $this-$#@62;dbusername, $this-$#@62;dbpassword);
Case 2;
Using functions that support SQL Server
Case 3;
Using functions that support ODBC
}
}
Turn off the number library function
function Dbclose ($dataHandle) {
Switch ($this-$#@62;dbid)
{
Case 1;
Mysql_close ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using functions that support ODBC
}
}

Executing SQL statement functions
function Dbquery ($dbName, $sql, $dbHandle) {
Switch ($this-$#@62;dbid)
{
Case 1;
Return @mysql_db_query ($dbName, $sql, $dbHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using functions that support ODBC
}
}

Retrieves the current record function of a SQL return value
function Dbfetchrow ($dataHandle, $offset =-1) {
Switch ($this-$#@62;dbid)
{
Case 1;
@mysql_data_seek ($dataHandle, $offset);
Return @mysql_fetch_row ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using functions that support ODBC
}
}

Returns the number of retrieved records function
function Dbnumrows ($dataHandle) {
Switch ($this-$#@62;dbid)
{
Return @mysql_num_rows ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using functions that support ODBC
}
}


Returns the number of retrieved columns function
function Dbnumcols ($dataHandle) {
Switch ($this-$#@62;dbid)
{
Case 1;
Return @mysql_num_fields ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using functions that support ODBC
}
}
}


The use of the instructions is as follows:

The Dbinterface class is used in the program to give birth to an object $test=new dbinterface;

Setting parameters
test-$#@62; $dbUsername; user name
test-$#@62, $dbPassword, password
test-$#@62; $dbHost; host
void Setparameter (string host, string username, string password);

Database connection: Dbhandle test-$#@62;dbconnect ();

Return value: Fasle, database connection error
$#@62;0, database connection handle

Database shutdown: void Test-$#@62;dbclose (Dbhandle);

Table action: int test-$#@62;dbquery (string databasename, String sql,dbhandle); Execute SQL statement

Return value: False, SQL execution error
$#@62;0, SQL executes correctly, pointing to the SQL return value,

Data manipulation: int test-$#@62;dbfetchrow (datahandle,int offset); Retrieves the current record of the SQL return value, and after successful execution, the pointer moves to the next record
int test-$#@62;dbnumrows (datahandle); Gets the number of records obtained after SQL execution (primarily a SELECT statement)
int Test-$#@62;dbnumcols (datahandle); Gets the number of record fields obtained after SQL execution (primarily a SELECT statement)

Now we are sending an example to explain:

The database uses Mqsql: Its host name is "localhost" and the user name is "root" and password "".
In MySQL, there is a TestDB database and the table table1 in it, which includes: Name and pay two fields

-----
$#@60; html$#@62;$#@60; head$#@62;
$#@60; title$#@62;test$#@60;/title$#@62;
$#@60;/head$#@62;$#@60; body$#@62;
$#@60; p$#@62; $#@60;? PHP
require ("testdb.inc");//Load Dbinterface class
$test = new dbinterface;//generate an object with class Dbinterface
$test-$#@62; Setparameter ("localhost", "root", "")//Set database parameter
$db = $test-$#@62;dbconnect ()//connection database
$Query = "Select Name,pay from table;/Set SQL statement
$temp _result = $test-$#@62;dbquery ("TestDB", $Query, $db);//Perform data Main library operations
Echo $#@60;br$#@62 ;";
$ls _num = $test-$#@62;dbnumrows ($temp _result);//Get the number of records for query results
Echo $ls _num;
echo "$#@60;br$#@62;";
if (ls_num$#@62;0)
{
$ls _col = $test-$#@62;dbnumcols ($db);//Get the number of columns in the table
Echo $ls _col;
echo "$#@60;br$#@62;";
$cate _result= $test-$#@62;dbfetchrow ($temp _result,0);//Get the first row of the number of records
$hcid = $cate _result[0];//Get the value of name
$ Hcate= $cate _result[1];//Gets the value of pay
Echo $hcid;
echo "$#@60;br$#@62;";
Echo $hcate;
}

? $#@62;
$#@60;hr$#@62;
$#@60; address$#@62;$#@60;/address$#@62;
$#@60;/body$#@62;$#@60;/html$#@62;
This is a simple application of the encapsulated class to complete the operation of the database. If you want to manipulate other databases, you only need to modify the dbid variable in the Dbinterface class.



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.