本文介绍了维护所有PHP文件中的$ dbh(数据库句柄)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几种方法可以在所有php文件中维护$ dbh(数据库句柄),这样,一旦创建$ dbh,我就可以在任何时间从任何php文件查询和更新数据库,而无需登录.

How many ways are there to maintain $dbh (database handle) across all php files,so that once $dbh created, I can query and update database from any php file and any time, without having to log in.

1)在每个php文件中全局应用$ dbh吗?2)在被调用函数的参数中应用$ dbh吗?3)?

1) apply $dbh global in every php file ?2) apply $dbh in the parameter of the called function ? 3) ?

还有什么其他方法可以查询和更新,而无需再次登录,这是更好,更简单的方法.

What other ways are there to, so as to query and update without ever having to log in again and which is better and simple.

感谢您的输入.

致谢克莱门特

推荐答案

在创建$ dbh的文件中,放入

In the file that creates $dbh, put

global $dbh;
...
$dbh = new DatabaseClass();
$dbh->example_login("user","pass");
...

在每个要使用$ dbh的文件和函数中,放

In every file and function that wants to use $dbh, put

global $dbh;
...
$result = $dbh->query("SELECT * FROM XYZ");
...

首先将$dbh标记为全局.您也可以使用单例类型模式,尽管这在PHP中被认为是一种不好的做法.

at the start to mark $dbh as global. You could also use a singleton type pattern, although this is considered bad practice in PHP.

这篇关于维护所有PHP文件中的$ dbh(数据库句柄)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 09:35