分离各个页面的数据库连接
时间:2007-04-13
来源:互联网
我们在开发php的时候经常的要用到数据库的连接,但是数据库的连接一般是很好资源的,为此,可以设计单例模式,以此来保证数据库的连接是只是用一个,无论你在那里要于数据库交互,这样就保证了应用程序的整体的性能比一般的模式优一点,在面向过程的习惯中很难的使用单例模式,当然这是对于习惯于面向对象的变成人员来说的,如果使用过程变成的模式是完全可以的,之不过我们还是要设置全局的变量来保存数据库的连接,但是这样的华就会在我们的成语中有所限制,假设你的 程序可能包含很多的文件,总的行数超过3000行或者是更多的华,你就要面对变量的问题而焦虑了,为此改用oo的方式还是很不错的选择,现在我就把我的实现方式于大家分享,据提到你的应用可能还不可以,你可以作必要的修改一服和你的程序。现便是代码的实现。
欢迎你来批评。
<?php
/***************************************************************************
* Copyright (C) 2007 by 耿鸿飞 *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","test"):
class DBConnect()
{
private static $DB;
private function & getCon()
{
if(self::DB == nuyll)
{
self::DB = &mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("连接数据库失败!");
mysql_select_db(self::DB,DB_NAME);
}
return self::DB;
}
/*****************************************************************************
* insert or update data to database;
*
*/
public upDate($sql)
{
return mysql_query($sql,this.getCon());
}
/**************************************************************************************
* query data from database and return data that type is array
*/
public query($sql)
{
$rs = mysql_query($sql,this.getCon());
$rows = array();
$i = 0;
while($row = mysql_fetch_array($rs))
{
rows[$i] = $row;
$i++;
}
mysql_free_result($rs);
return $rows;
}
/********************************************************************
* query database and return data rows;
*/
public query_num_rows($sql)
{
$rs = mysql_query($sql,this.getCon());
return mysql_num_row($rs);
}
/*******************************************************************
* close dbconnection;
*/
public colse()
{
mysql_colse(this.getCon());
self::DB = null;
}
}
?>
欢迎你来批评。
<?php
/***************************************************************************
* Copyright (C) 2007 by 耿鸿飞 *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","");
define("DB_NAME","test"):
class DBConnect()
{
private static $DB;
private function & getCon()
{
if(self::DB == nuyll)
{
self::DB = &mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("连接数据库失败!");
mysql_select_db(self::DB,DB_NAME);
}
return self::DB;
}
/*****************************************************************************
* insert or update data to database;
*
*/
public upDate($sql)
{
return mysql_query($sql,this.getCon());
}
/**************************************************************************************
* query data from database and return data that type is array
*/
public query($sql)
{
$rs = mysql_query($sql,this.getCon());
$rows = array();
$i = 0;
while($row = mysql_fetch_array($rs))
{
rows[$i] = $row;
$i++;
}
mysql_free_result($rs);
return $rows;
}
/********************************************************************
* query database and return data rows;
*/
public query_num_rows($sql)
{
$rs = mysql_query($sql,this.getCon());
return mysql_num_row($rs);
}
/*******************************************************************
* close dbconnection;
*/
public colse()
{
mysql_colse(this.getCon());
self::DB = null;
}
}
?>
作者: eoe2005 发布时间: 2007-04-12
我靠啊!~~
你写的对不对啊!
好像是火星人的PHP写法!
你写的对不对啊!
好像是火星人的PHP写法!
作者: Lukin 发布时间: 2007-04-13
这是很好的语法习惯,当你经过了长期的编成生涯之后你就会觉得他的好处
作者: eoe2005 发布时间: 2007-04-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28