未加星标

PHP数据库处理封装类实例

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 | 作者 红领巾 ] 0人收藏点击收藏

本文实例讲述php数据库处理封装类。分享给大家供大家参考,具体如下:

mysql的操作相关类,检查并使用了mysqli

<?php
//sample15_12.php
class mydb {
private $user;
private $pass;
private $host;
private $db;
//Constructor function.
public function __construct (){
$num_args = func_num_args();
if($num_args > 0){
$args = func_get_args();
$this->host = $args[0];
$this->user = $args[1];
$this->pass = $args[2];
$this->connect();
}
}
//Function to tell us if mysqli is installed.
private function mysqliinstalled (){
if (function_exists ("mysqli_connect")){
return true;
} else {
return false;
}
}
//Function to connect to the database.
private function connect (){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db = new mysqli ($this->host,$this->user,$this->pass)){$exceptionstring = "Error connection to database: <br />";$exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error();throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!$this->db = mysql_connect ($this->host,$this->user,$this->pass)){$exceptionstring = "Error connection to database: <br />";$exceptionstring .= mysql_errno() . ": " . mysql_error();throw new exception ($exceptionstring);
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to select a database.
public function selectdb ($thedb){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db->select_db ($thedb)){$exceptionstring = "Error opening database: $thedb: <br />";$exceptionstring .= $this->db->errno . ": " . $this->db->error;throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!mysql_select_db ($thedb, $this->db)){$exceptionstring = "Error opening database: $thedb: <br />";$exceptionstring .= mysql_errno() . ": " . mysql_error();throw new exception ($exceptionstring);
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to perform a query.
public function execute ($thequery){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db->query ($thequery)){$exceptionstring = "Error performing query: $thequery: <br />";$exceptionstring .= $this->db->errno . ": " . $this->db->error;throw new exception ($exceptionstring);
} else {echo "Query performed correctly: " . $this->db->affected_rows . " row(s) affected.<br />";
}
//Mysql functionality.
} else {
if (!mysql_query ($thequery, $this->db)){$exceptionstring = "Error performing query: $thequery: <br />";$exceptionstring .= mysql_errno() . ": " . mysql_error();throw new exception ($exceptionstring);
} else {echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to return a row set.
public function getrows ($thequery){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if ($result = $this->db->query ($thequery)){$returnarr = array ();while ($adata = $result->fetch_array ()){ $returnarr = array_merge ($returnarr,$adata);}return $returnarr;
} else {$exceptionstring = "Error performing query: $thequery: <br />";$exceptionstring .= $this->db->errno . ": " . $this->db->error;throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!$aquery = mysql_query ($thequery)){$exceptionstring = "Error performing query: $thequery: <br />";$exceptionstring .= mysql_errno() . ": " . mysql_error();throw new exception ($exceptionstring);
} else {$returnarr = array ();while ($adata = mysql_fetch_array ($aquery)){ $returnarr = array_merge ($returnarr,$adata);}return $returnarr;
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to close the database link.
public function __destruct() {
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db->close()){$exceptionstring = "Error closing connection: <br />";$exceptionstring .= $this->db->errno . ": " . $this->db->error;throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!mysql_close ($this->db)){$exceptionstring = "Error closing connection: <br />";$exceptionstring .= mysql_errno() . ": " . mysql_error();throw new exception ($exceptionstring);
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
}
//Now, let us create an instance of mydb.
$mydb = new mydb ("localhost","root","");
//Select a database to use.
$mydb->selectdb ("wps");
//Now, let's perform an action.
//$adata = $mydb->execute ("UPDATE cd SET title='Hybrid Theory' WHERE cdid='2'");
//Then, let's try to return a row set.
$adata = $mydb->getrows ("SELECT * FROM wp_terms");
for ($i = 0; $i < count ($adata); $i++){
echo $adata[$i] . "<br />";
}
$mydb->selectdb("test");
$result = $mydb->execute("UPDATE user SET age = 23 WHERE id = 2");
echo "<br />";
echo $result;
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《PHP基于pdo操作数据库技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php+Oracle数据库程序设计技巧总结》、《php+mssql数据库程序设计技巧总结》、《php+redis数据库程序设计技巧总结》、《php+mysqli数据库程序设计技巧总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

分页:12
转载请注明
本文标题:PHP数据库处理封装类实例
本站链接:http://www.codesec.net/view/524344.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(47)