PHP中对数据库操作的封装
封装|数据|数据库
现在我们把其封装在dbfz.inc中,其设计如下:
$#@60;?
class dbInterface{ var $dbID=1; //用于确定当前操作的数据库,当dbID为1代表MySql,当为 2代表 SQL Server,为3时为ODBC或其它。
var $dbHost; //数据库所在主机域名
var $dbUsername; //数据库用户名
var $dbPassword; //用户密码
//设置主机、用户名及密码函数
function setParameter($host,$username,$password){
$this-$#@62;dbUsername=$username;
$this-$#@62;dbHost=$host;
$this-$#@62;dbPassword=$password;
}
//联接数库函数
function dbConnect(){
switch($this-$#@62;dbID)
{
case 1;
return @mysql_connect($this-$#@62;dbHost,$this-$#@62;dbUsername,$this-$#@62;dbPassword);
case 2;
//用支持SQL Server的函数
case 3;
//用支持ODBC的函数
}
}
//关闭数库函数
function dbClose($dataHandle){
switch($this-$#@62;dbID)
{
case 1;
mysql_close($dataHandle);
case 2;
//用支持SQL Server的函数
case 3;
//用支持ODBC的函数
}
}
//执行SQL语句函数
function dbQuery($dbName,$sql,$dbHandle){
switch($this-$#@62;dbID)
{
case 1;
return @mysql_db_query($dbName,$sql,$dbHandle);
case 2;
//用支持SQL Server的函数
case 3;
//用支持ODBC的函数
}
}
//检索SQL返回值的当前记录函数
function dbFetchrow($dataHandle,$offset=-1){
switch($this-$#@62;dbID)
{
case 1;
@mysql_data_seek($dataHandle,$offset);
return @mysql_fetch_row($dataHandle);
case 2;
//用支持SQL Server的函数
case 3;
//用支持ODBC的函数
}
}
//返回检索记录数函数
function dbNumrows($dataHandle){
switch($this-$#@62;dbID)
{
return @mysql_num_rows($dataHandle);
case 2;
//用支持SQL Server的函数
case 3;
//用支持ODBC的函数
}
}
//返回检索列数函数
function dbNumcols($dataHandle){
switch($this-$#@62;dbID)
{
case 1;
return @mysql_num_fields($dataHandle);
case 2;
//用支持SQL Server的函数
case 3;
//用支持ODBC的函数
}
}
}
现把使用说明如下:
在程序中用dbInterface类生一个对象$test=new dbInterface;
设置参数
test-$#@62;$dbUsername ;用户名
test-$#@62;$dbPassword;密码
test-$#@62;$dbHost;主机
void setParameter(string host, string username, string password);
数据库连接:dbhandle test-$#@62;dbConnect();
返回值:fasle ,数据库连接错误
$#@62;0, 数据库连接句柄
数据库关闭:void test-$#@62;dbClose(dbhandle);
表操作:int test-$#@62;dbQuery(string databasename, string sql,dbhandle);执行SQL语句
返回值: false, SQL执行错误
$#@62;0, SQL执行正确, 同时指向SQL返回值,
数据操作:int test-$#@62;dbFetchrow(dataHandle,int offset);检索SQL返回值的当前记录,成功执行后,指针移向下一条记录
int test-$#@62;dbNumrows(dataHandle); 取得SQL执行后(主要为SELECT语句)获得的记录数
int test-$#@62;dbNumcols(dataHandle); 取得SQL执行后(主要为SELECT语句)获得的记录字段数
现在我们发一个例了讲解:
数据库采用MQSQL:其主机名为 "localhost",用户名为"root"和密码""。
在mysql中有一个testdb数据库及其中的表table1,表包括:name和pay两个字段
-----
$#@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"); //装载dbInterface类
$test = new dbInterface;//用类dbInterface生成一个对象
$test-$#@62;setParameter("localhost","root","");//设置数据库参数
$db = $test-$#@62;dbConnect();//连接数据库
$Query = "SELECT name,pay FROM table ";//设置SQL语句
$temp_result = $test-$#@62;dbQuery("testdb",$Query,$db);//执行数据主库操作
echo "$#@60;br$#@62;";
$ls_num = $test-$#@62;dbNumrows($temp_result); //取得查询结果的记录数
echo $ls_num;
echo "$#@60;br$#@62;";
if (ls_num$#@62;0 )
{
$ls_col = $test-$#@62;dbNumcols($db); //取得表的列数
echo $ls_col;
echo "$#@60;br$#@62;";
$cate_result=$test-$#@62;dbFetchrow($temp_result,0);//取得记录数的第一行
$hcid=$cate_result[0];// 取得name的值
$hcate=$cate_result[1];//取得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;
这就是一个简单的应用封装的类来完成对数据库的操作。如果要操作其它数据库只需要修改dbInterface类中的dbID变量即可.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

Katsuhiro Harada, the Tekken series director, once seriously tried to bring Colonel Sanders into the iconic fighting game. In an interview with TheGamer, Harada revealed that he pitched the idea to KFC Japan, hoping to add the fast-food legend as a g

Xiaomi will shortly launch the Mijia Graphene Oil Heater in China. The company recently ran a successful crowdfunding campaign for the smart home product, hosted on its Youpin platform. According to the page, the device has already started to ship to

Garmin is ending the month with a new set of stable updates for its latest high-end smartwatches. To recap, the company released System Software 11.64 to combat high battery drain across the Enduro 3, Fenix E and Fenix 8 (curr. $1,099.99 on Amazon).

With a history of over one decade, Manjaro is regarded as one of the most user-friendly Linux distros suitable for both beginners and power users, being easy to install and use. Mostly developed in Austria, Germany, and France, this Arch-based distro

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

The BD790i SE is MINISFORUM's latest Mini-ITX motherboard option, having released the BD770i andBD790iin October 2023 and February 2024, respectively. As far as we can tell, the BD790i SE effectively replaces theBD770i, which MINISFORUM has now stopp
