Domanda

I have just recently acquired the service side of a medium size project. The former developer has all of his functions as separate php scripts instead of classes (func1.php, func2.php, etc)... All these 'functions' make a reference to mysqli_connect via referencing the actual 'databaseonnection.php' file. This is creating a new connection every time any of the scripts run (every time I have to call a function) and I don't want to do that. I was thinking about having a persistent connection, but I'm worried about it getting out of hands as the project is growing more and more every day. So, has anyone ever encountered a similar situation? What is the best way to handle my connection to the database? Any suggestions would be greatly appreciated.

È stato utile?

Soluzione

From the docs for mysql_connect. If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.

Altri suggerimenti

EDIT: I'm sorry I thought you wanted connectivity help. There is no way except to move all those "functions" into one file where the connection is for them only.

I create a con.php file where my PDO connection is established then include that file anywhere you wish to use a connection Here is the base for a PDO connection:

$PDO = new PDO("mysql:host=localhost;dbname=dbname", "user_name", "password");

Here is my notes on using the PDO object to make prepared queries. There is more than you need below but good luck.

Within your PHP file that needs a connection: 1: include('con.php');

2:  $datas = $PDO->prepare(SELECT * FROM table WHERE title LIKE :searchquery);
    // prepare method creates and returns a PDOstatment object ( print_r($datas); ) which contains an execute() method
    // PDOstatment object has its own methods ie. rowCount()

    // $datas->bindValue(':search', '% . $search . %', )
    // Optional - Manually bind value. see http://php.net/manual/en/pdostatement.bindparam.php

3: $datas->execute( array(':searchquery' => $searchquery . '%'));
    // pass in values that need to be bound AND EXECUTE.

    // There are 17 ways to "fetch" data with the PDO object.
4: $datas-fetchALL(PDO::FETCH_OBJ);

close a pdo connection by the handle:

$PDO = null;

I think you'll be much better off using PDO as opposed to the old MYSQL functions e.g. mysql_connect. It's much more robust an interface.

Below is the basic code to do this:

$db_handle = new PDO("mysql:host=".$db_host.";dbname=".$db_name.";port=".$db_port."", $db_username, $db_password, $connect_options);

where $db_handle is the PDO object representing the database connection, $db_host is your hostname [usually localhost], $db_name is the name of your database, $db_port is the database port number [usually 3306], $db_username and $db_password are your database user access credentials, and $connect_options are optional driver-specific connection options.

To enable persistent connections you need to set the driver-specific connection option for it before opening the connection: $connect_options = array(PDO::ATTR_PERSISTENT => true); then execute the earlier database connection code.

You can get more information on this from the PHP Docs here: http://www.php.net/manual/en/pdo.construct.php and http://php.net/manual/en/pdo.connections.php.

Regarding creating persistent connections, I would suggest that you close every database connection you open at the end of your script (after all your database operations of course) by nullifying your database handle: $db_handle = NULL;. You should do this whether you opened a persistent connection or not. It sounds counter-intuitive, but I believe you should free up any database resources when your script is done.

The performance disadvantages of doing this [from my experience] are neglible for most applications. This is obviously an arguable assertion and you may also find the following link helpful in further clarifying your strategy in this regard:

Persistent DB Connections - Yea or Nay?

Happy coding!

if you have very complex project and need big budget to re-design, and prefer very simple alteration then

1) stay in mysqli_connect 2) move the database connection to header of your script. 3) remove the function databse close() on that functions. 4) remove the connection link variables, it wont needed for single database. 5) close the database on end of footer.

By this way, database connection establish when starting your script and after all queries, it will be closed on footer. your server can handle the connections without closing/re-open by using keepalive method. basically default keepalive value is 30 to 90 seconds.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top