开发者

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

开发者 https://www.devze.com 2023-01-15 08:03 出处:网络
Background: We have a class which is used for accessing the mysql database. The methods within this class form a new mysql connection if a link isnt supplied as a parameter.

Background:

  • We have a class which is used for accessing the mysql database. The methods within this class form a new mysql connection if a link isnt supplied as a parameter.
  • 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. (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.

I think I have the following choices:

1. Revise the class methods' code to use the class's internal link variable. Declare the database object once as a global object. Revise code usage all over the application to use this global variable.

2. Re开发者_开发知识库vise the class methods' code to use the class's internal link variable. Save the "MySQL link identifier returned by mysql_connect()" in a globally accessible variable and then use that when creating the database object. 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? Any potential issues?


Take a look at dependency injection and inversion of control containers.


Do this:

  1. Remove all mysql_close() statements.
  2. Convert all mysql_connect() to mysql_pconnect() to create persistent connections.
0

精彩评论

暂无评论...
验证码 换一张
取 消