php面象对象数据库操作类实例
作者:shichen2014 时间:2023-10-13 22:33:05
本文实例讲述了php面象对象数据库操作类。分享给大家供大家参考。
具体实现代码如下:
//此处构造一个数据库操作类,封装所有数据库操作
//可以扩展便于后台管理程序的使用
Class MySQLDB
{
var $host;
var $user;
var $passwd;
var $database;
var $conn;
//利用构造函数实现变量初始化
//同时连接数据库操作
function MySQLDB($host,$user,$password,$database)
{
$this->host = $host;
$this->user = $user;
$this->passwd = $password;
$this->database = $database;
$this->conn=mysql_connect($this->host, $this->user,$this->passwd) or
die("Could not connect to $this->host");
mysql_select_db($this->database,$this->conn) or
die("Could not switch to database $this->database");
}
//该函数用来关闭数据库连接
function Close()
{
MySQL_close($this->conn);
}
//该函数实现数据库查询操作
function Query($queryStr)
{
$res =Mysql_query($queryStr, $this->conn) or
die("Could not query database");
return $res;
}
//该函数返回记录集
function getRows($res)
{
$rowno = 0;
$rowno = MySQL_num_rows($res);
if($rowno>0)
{
for($row=0;$row<$rowno;$row++ )
{
$rows[$row]=MySQL_fetch_array($res);
//本来为MySQL_fetch_row,但是不能以数组的方式来提取,只能用索引
//这样可以用索引和名称,更为方便
}
return $rows;
}
}
//该函数取回数据库记录数
function getRowsNum($res)
{
$rowno = 0;
$rowno = mysql_num_rows($res);
return $rowno;
}
//该函数返回数据库表字段数
function getFieldsNum($res)
{
$fieldno = 0;
$fieldno = mysql_num_fields($res);
return $fieldno;
}
//该函数返回数据库表字段名称集
function getFields($res)
{
$fno = $this->getFieldsNum($res);
if($fno>0)
{
for($i=0;$i<$fno;$i++ )
{
$fs[$i]=MySQL_field_name($res,$i);//取第i个字段的名称
}
return $fs;
}
}
}
//使用时直接require该文件,然后实例化:
$SqlDB = new MySQLDB("localhost","root","root","testdb");
$sql = "select * from tableX...";
$result = $SqlDB->Query($sql);//查询
$rs = $SqlDB->getRows($result);//获得记录集
$num = $SqlDB->getRowsNum($result);//获得记录数
...剩下的操作就是循环取值,
for($i=0;$i<$num;$i++){
echo($rs[$i]["字段名"]);
}
...
最后不要忘记关闭数据路连接
$SqlDB->Close();
当然这句可以不要,php会自动注销!但是这样能够养成一个好的习惯,最好还是加上!其他自己类推。
希望本文所述对大家的PHP程序设计有所帮助。
标签:php,面象对象,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server 中调整自增字段的当前初始值
2024-01-27 13:53:01
EXCEL数据上传到SQL SERVER中的简单实现方法
2024-01-27 15:28:06
用js控件div的滚动条,让它在内容更新时自动滚到底部的实现方法
2024-04-19 09:45:30
MySql完整卸载的四个步骤详解
2024-01-18 00:30:52
![](https://img.aspxhome.com/file/2023/0/80020_0s.jpg)
使用keras实现BiLSTM+CNN+CRF文字标记NER
2022-05-01 04:46:22
Python画图实现同一结点多个柱状图的示例
2021-04-21 23:26:04
![](https://img.aspxhome.com/file/2023/5/115695_0s.jpg)
Python 处理数据的实例详解
2021-05-31 21:29:35
ASP获取ACCESS数据库表名及结构的代码
2011-04-15 10:50:00
Go实现简易RPC框架的方法步骤
2024-04-26 17:16:33
如何在python中实现ECDSA你知道吗
2023-03-14 14:07:17
![](https://img.aspxhome.com/file/2023/5/110045_0s.jpg)
教你如何在CI框架中使用 .htaccess 隐藏url中index.php
2023-11-14 11:49:57
python处理文本文件实现生成指定格式文件的方法
2022-01-07 21:41:43
浅谈Django中view对数据库的调用方法
2024-01-17 02:13:28
windows下安装php的memcache模块的方法
2023-11-20 05:47:38
![](https://img.aspxhome.com/file/2023/5/104165_0s.png)
python 2.6.6升级到python 2.7.x版本的方法
2022-09-25 15:07:45
2008年Logo设计10大趋势
2008-02-28 13:06:00
![](https://img.aspxhome.com/file/UploadPic/20082/28/200822813204572s.gif)
Go语言中使用反射的方法
2023-07-22 22:31:48
python读取文本中数据并转化为DataFrame的实例
2021-10-24 13:08:28
![](https://img.aspxhome.com/file/2023/5/77785_0s.jpg)
微信小程序开发自定义tabBar实战案例(定制消息99+小红心)
2024-05-22 10:32:10
![](https://img.aspxhome.com/file/2023/7/123447_0s.gif)
Select count(*)、Count(1)和Count(列)的区别及执行方式
2024-01-28 14:43:04
![](https://img.aspxhome.com/file/2023/8/92768_0s.png)