简体   繁体   English

使用mysql_connect()返回的MySQL链接作为全局变量?

[英]Using the MySQL link returned by mysql_connect() as a global variable?

Background : 背景

  • We have a class which is used for accessing the mysql database. 我们有一个用于访问mysql数据库的类。 The methods within this class form a new mysql connection if a link isnt supplied as a parameter. 如果未提供链接作为参数,则此类中的方法将形成新的mysql连接。
  • The way we've used the class in the system (not the database class) is to declare a database object and call the appropriate method and let the object scope end whenever it does. 我们在系统中使用该类(而不是数据库类)的方式是声明一个数据库对象并调用适当的方法,并让对象作用域结束。
  • We ran into a problem with code in the system (not the database class) that was creating a connection, inserting a record, closing the connection and then opening a new connection in order to fetch the max (id) to retrieve the last inserted id. 我们在系统中(而不是数据库类)的代码遇到问题,该代码正在创建连接,插入记录,关闭连接然后打开新连接以获取最大值(id)来检索最后插入的id 。 (Obviously this wouldnt scale.) (显然这不会扩展。)

Problem : 问题

I'm trying to correct the code to replace the max (id) usage with mysql_insert_id in the appropriate functions and I need to revise the code structure in one of the following ways to PASS the database connection link. 我正在尝试更正代码,以在适当的函数中用mysql_insert_id替换最大(id)用法,并且我需要通过以下方式之一修改代码结构,以通过数据库连接链接。

I think I have the following choices: 我认为我有以下选择:
1. Revise the class methods' code to use the class's internal link variable. 1.修改类方法的代码以使用类的内部链接变量。 Declare the database object once as a global object. 一次将数据库对象声明为全局对象。 Revise code usage all over the application to use this global variable. 修改整个应用程序中的代码用法以使用此全局变量。
2. Revise the class methods' code to use the class's internal link variable. 2.修改类方法的代码以使用类的内部链接变量。 Save the "MySQL link identifier returned by mysql_connect()" in a globally accessible variable and then use that when creating the database object. 将“ mysql_connect()返回的MySQL链接标识符”保存在全局可访问变量中,然后在创建数据库对象时使用它。 Minimal impact to use of the object and its methods. 对使用对象及其方法的影响最小。

Is there any other option? 还有其他选择吗? I'm edging for (2) because it has less of a code churn - but should I be doing so? 我正在争取(2),因为它的代码搅动较少-但我应该这样做吗? Any potential issues? 有潜在的问题吗?

Do this: 做这个:

  1. Remove all mysql_close() statements. 删除所有的mysql_close()语句。
  2. Convert all mysql_connect() to mysql_pconnect() to create persistent connections. 将所有mysql_connect()转换为mysql_pconnect()以创建持久连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM