Data recovery software easyrecovery The encapsulation of database operations in PHP

Source: Internet
Author: User
Encapsulation of database operations in PHP
In the dynamic network design, many involve the operation of the database, but sometimes it is necessary to use other back-end database, you need to modify the program a lot. This is a tedious, time-consuming and error-prone work. In fact, we can use the class in PHP to implement the encapsulation of database operations, so that the written program can be very small changes to complete the background database changes.
Now we enclose it in Dbfz.inc, which is designed as follows:
Class dbinterface{var $dbID = 1;//is used to determine the current operation of the database when DbID is 1 for MySQL, when 2 for SQL Server, and 3 for ODBC or others.
var $dbHost; Host domain name where the database resides
var $dbUsername; Database user Name
var $dbPassword; User password
Set host, user name, and password functions
function Setparameter ($host, $username, $password) {
$this->dbusername= $username;
$this->dbhost= $host;
$this->dbpassword= $password;
}//Join number library functions
function Dbconnect () {
Switch ($this->dbid)
{
Case 1;
Return @mysql_connect ($this->dbhost, $this->dbusername, $this->dbpassword);
Case 2;
Using functions that support SQL Server
Case 3;
Using ODBC-enabled functions
}
}
Turn off the library function
function Dbclose ($dataHandle) {
Switch ($this->dbid)
{
Case 1;
Mysql_close ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using ODBC-enabled functions
}
}
Execute SQL statement function
function Dbquery ($dbName, $sql, $dbHandle) {
Switch ($this->dbid)
{
Case 1;
Return @mysql_db_query ($dbName, $sql, $dbHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using ODBC-enabled functions
}
}
Retrieving the current record function for the SQL return value
function Dbfetchrow ($dataHandle, $offset =-1) {
Switch ($this->dbid)
{
Case 1;
@mysql_data_seek ($dataHandle, $offset);
Return @mysql_fetch_row ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using ODBC-enabled functions
}
}
Returns the number of retrieved records function
function Dbnumrows ($dataHandle) {
Switch ($this->dbid)
{
Case 1;
Return @mysql_num_rows ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using ODBC-enabled functions
}
}
Returns the number of columns to retrieve function
function Dbnumcols ($dataHandle) {
Switch ($this->dbid)
{
Case 1;
Return @mysql_num_fields ($dataHandle);
Case 2;
Using functions that support SQL Server
Case 3;
Using ODBC-enabled functions
}
}
}
The following instructions are available:
The Dbinterface class is used in the program to give birth to an object $test=new dbinterface;
Setting parameters
test-> $dbUsername; user name
test-> $dbPassword; password
Test-> $dbHost; host
void Setparameter (string host, string username, string password);
Database connection: Dbhandle test->dbconnect ();
Return value: Fasle, database connection error
>0, database connection handle
Database shutdown: void Test->dbclose (Dbhandle);
Table operations: Int Test->dbquery (string databasename, String sql,dbhandle); Execute SQL statement
Return value: False, SQL execution error
>0, SQL executes correctly, pointing to the SQL return value,
Data manipulation: int test->dbfetchrow (datahandle,int offset); Retrieves the current record of the SQL return value, and after successful execution, the pointer moves down one record
int test->dbnumrows (datahandle); Number of records obtained after SQL execution (primarily SELECT statements)
int Test->dbnumcols (datahandle); Number of record fields obtained after SQL execution (primarily SELECT statements)
Now we are going to send an example to explain:
The database uses Mqsql: Its host name is "localhost" and the user name is "root" and "password".
There is a testdb database in MySQL and the table table1 in it, including: Name and pay two fields
-----

<title>Test</title>

Require ("Testdb.inc"); Load Dbinterface class
$test = new dbinterface;//generates an object with class Dbinterface
$test->setparameter ("localhost", "root", "");//Set Database parameters
$db = $test->dbconnect ();//Connect to Database
$Query = "Select Name,pay from table";//Set SQL statement
$temp _result = $test->dbquery ("TestDB", $Query, $db);//Perform data Main library operations
echo "
";
$ls _num = $test->dbnumrows ($temp _result); Number of records to get query results
echo $ls _num;
echo "
";
if (ls_num>0)
{
$ls _col = $test->dbnumcols ($db); Gets the number of columns in the table
echo $ls _col;
echo "
";
$cate _result= $test->dbfetchrow ($temp _result,0);//Get the first line of records
$hcid = $cate _result[0];//Gets the value of name
$hcate = $cate _result[1];//The value of pay
Echo $hcid;
echo "
";
Echo $hcate;
}
?>



This is a simple application wrapper 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.

The above describes the data recovery software easyrecovery PHP in the package of database operations, including the data recovery software easyrecovery aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.