我应该将 MySQL 数据库连接放在 PHP 函数中吗?

发布于 2024-11-06 21:02:26 字数 54 浏览 1 评论 0原文

将我的 MySQL 数据库连接放置在 PHP 函数中以便其他 PHP 函数可以使用它是否安全?

Is it safe to place my MySQL Database Connection inside a PHP Function so that other PHP functions can use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

—━☆沉默づ 2024-11-13 21:02:26

是的。这是个好主意。

我什至把我的放在一个班级里。

class database {

     protected $databaseLink;

     function __construct($mysql_db, $mysql_user, $mysql_pass, $mysql_dbName){
            $this->databaseName = $mysql_dbName;
            $this->database = $mysql_db;
            $this->mysql_user = $mysql_user;
            $this->mysql_pass = $mysql_pass;
            $this->openConnection();
      }

      function openConnection(){
            $this->databaseLink = mysql_connect($this->database, $this->mysql_user, $this->mysql_pass);
            mysql_select_db($this->databaseName, $this->databaseLink);
      }

      function get_link(){
            return $this->databaseLink;
      }

}

然后你可以这样做:

 $db = new database('database', 'username', 'password', 'dbName');

 //to do a query:
 mysql_query("some query", $db->get_link());

Yes. it is a good idea.

I even place mine in a class.

class database {

     protected $databaseLink;

     function __construct($mysql_db, $mysql_user, $mysql_pass, $mysql_dbName){
            $this->databaseName = $mysql_dbName;
            $this->database = $mysql_db;
            $this->mysql_user = $mysql_user;
            $this->mysql_pass = $mysql_pass;
            $this->openConnection();
      }

      function openConnection(){
            $this->databaseLink = mysql_connect($this->database, $this->mysql_user, $this->mysql_pass);
            mysql_select_db($this->databaseName, $this->databaseLink);
      }

      function get_link(){
            return $this->databaseLink;
      }

}

Then you can just do:

 $db = new database('database', 'username', 'password', 'dbName');

 //to do a query:
 mysql_query("some query", $db->get_link());
耳钉梦 2024-11-13 21:02:26

您可能不想在常规 PHP 函数 中创建它。如果您将有多个其他函数在同一脚本中访问数据库连接,请记住,如果您执行类似

function get_db()
{
  $db = mysql_connect(...);
  return $db;
}

“Then”的操作,则每次调用它时,您都将创建并返回一个新连接。最好在类中为每个脚本创建一次作为 Singleton,并让类方法将数据库一个连接返回到调用它的任何函数。

编辑 尼尔的答案实际上很好地演示了单例模式。用更完整的代码答案打败我。

You probably don't want to create it in a regular PHP function. If you will have multiple other functions accessing the db connection in the same script, remember that if you do something like

function get_db()
{
  $db = mysql_connect(...);
  return $db;
}

Then each time you call it, you will be creating and returning a new connection. It may be better to create it once per script inside a class as a Singleton, and have a class method return the database one connection to any function that calls it.

EDIT Neal's answer actually demonstrates the singleton pattern for this nicely. Beat me to it with a more code-complete answer.

箹锭⒈辈孓 2024-11-13 21:02:26

这对于性能来说是一件好事。在调用该函数时连接到数据库将为 SQL Server 带来良好的性能。

It is a good thing for performance. Connecting to the database just when the function was called it will result a good performance for SQL server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文