使用PHP怎么封装一个数据库处理类

  介绍

这篇文章主要介绍了使用PHP怎么封装一个数据库处理类,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:

PHP有什么用

PHP是一个嵌套的缩写名称,指的是英文超级文本预处理语言(PHP:超文本预处理器)的缩写,它的语法混合了C、Java、Perl以及PHP自创新的语法,主要用来做网站开发,许多小型网站都用PHP开发,因为PHP是开源的,从而使PHP经得久不衰。

MySQL的操作相关类,检查并使用了mysqli吗?

& lt; php ?//sample15_12.php才能   class 才能;mydb  {   ,,,private  $用户;   ,,,private 通过美元;   ,,,private  $主机;   ,,,private  $ db;   ,,//Constructor 函数。   ,,,public  function  __construct  () {   ,,,,,num_args 美元;=,func_num_args ();   ,,,,,如果(num_args 美元;祝辞,0){   ,,,,,,,args 美元;=,func_get_args ();   ,,,,,,,这个→美元host =, args[0]美元;   ,,,,,,,这个→美元user =, args美元[1];   ,,,,,,,这个→美元pass =, args美元[2];   ,,,,,,,这个→美元connect ();   ,,,,,}   ,,,}   ,,,//Function 用tell  us  if  mysqli  is 安装。   ,,,private  function  mysqliinstalled  () {   ,,,,,if  (function_exists  (“mysqli_connect")) {   ,,,,,,,return 真实;   ,,,,,},{else    ,,,,,,,return 假;   ,,,,,}   ,,,}   ,,,//Function 用connect 用,数据库。   ,,,private  function  connect  () {   ,,,,,try  {   ,,,,,,//Mysqli 功能。   ,,,,,,,if  ($ this→mysqliinstalled ()) {   ,,,,,,,,,if  (! $ this→db =, new  mysqli  ($ this→主机、$ this→用户、$ this→通过)){   ,,,,,,,,,,,exceptionstring 美元;=,“Error  connection 用数据库:,& lt; br /祝辞;“;   ,,,,,,,,,,,exceptionstring 美元。=,mysqli_connect_errno(),只,:,“,只mysqli_connect_error ();   ,,,,,,,,,,,throw  new  exception  ($ exceptionstring);   ,,,,,,,,,}   ,,,,,,//Mysql 功能。   ,,,,,,,},{else    ,,,,,,,,,if  (! $ this→db =, mysql_connect  ($ this→主机、$ this→用户、$ this→通过)){   ,,,,,,,,,,,exceptionstring 美元;=,“Error  connection 用数据库:,& lt; br /祝辞;“;   ,,,,,,,,,,,exceptionstring 美元。=,mysql_errno(),只,:,“,只mysql_error ();   ,,,,,,,,,,,throw  new  exception  ($ exceptionstring);   ,,,,,,,,,}   ,,,,,,,}   ,,,,,},catch  (exception  $ e), {   ,,,,,,,echo  e→美元getmessage ();   ,,,,,}   ,,,}   ,,,//Function 用select  a 数据库。   ,,,public  function  selectdb  (thedb美元){   ,,,,,try  {   ,,,,,,//Mysqli 功能。   ,,,,,,,if  ($ this→mysqliinstalled ()) {   ,,,,,,,,,if  (! $ this→db→select_db  (thedb美元)){   ,,,,,,,,,,,exceptionstring 美元;=,“Error  opening 数据库:,thedb:美元,& lt; br /祝辞;“;   ,,,,,,,,,,,exceptionstring 美元。=,$ this→db→errno 只,:,“,只这→美元db→错误;   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

使用PHP怎么封装一个数据库处理类