首页 > 编程知识 正文

php数据库连接函数,php如何进入数据库

时间:2023-05-05 06:32:39 阅读:32486 作者:840

类singleton

{

//MYSQL数据库连接信息

常数MySQL hostname=' 127.0.0.1 ';

const MYSQLCHARSET='utf8 ';

私有静态$ instances=array (; //假系单壳

隐私$数据库; //数据库连接句柄

//*

* Description:私有化构造函数可防止外部对象实例化

*/

private function _ _ construct $ dbname、$username、$password )

{

//连接到数据库

$ this-db=new mysqli (self :3360 MySQL hostname、$username、$password、$dbname );

$ this-d B- set _ charset (self : MySQL charset;

if(mysqli_connect_errno ()

{

thrownewcirclemysqlexception (' error ',1000 );

}

}

//*

* Description:专用克隆函数,防止外部克隆对象

*/

私有函数_ _ clone (

{

}

//*

*描述:静态方法、单实例访问集成门户

* @return Singleton :返回APP应用程序中唯一的对象实例

*/

publicstaticfunctiongetinstance $ dbname、$username、$password )

{

$hash=(string ) $dbname;

if (! isset (自助: $ instances [ $ hash ] ) )

{

self :3360 $ instances [ $ hash ]=new self ($ dbname、$username、$password );

}

return self :3360 $ instances [ $ hash ];

}

//*

* Description:获取数据库专用方法的连接句柄

*/

公共函数获取数据库连接(

{

返回$ this-db;

}

//*

*如何关闭说明:数据库链接

*/

publicfunctionclosedbconnect ($ dbname ) )。

{

$hash=(string ) $dbname;

self :3360 $ instances [ $ hash ]-d B- close (;

unset (self :3360 $ instances [ $ hash ];

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。