Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I keep a db connection alive in PHP OO? 1

Status
Not open for further replies.

ElectronikBean

Programmer
Oct 10, 2002
24
0
0
GB
I have some data stored in MySQL DB and use a class object to retrieve the data on a single php page.

I want to create a single connection to the database, call the object's function several times to collect each individual peice of data, and then close the connection. So far, I have only been able code it to create a connection for every query processed... I wanted to cut down on the work that the back end db was doing. As the page calls the object several times, and therefore connects several times, this could mean a overhead I could do without.

Example...
getdata.php
--------------
Code:
<HTML>
<!-- HTML doc -->
<?php

// create the object
include "getdata.class";

$getdata = new getdata();

?>

<!--some html...-->

<?php $getdata->main("1") ?>

<!-- more html...-->

<?php $getdata->main("2") ?>

<!-- more html...-->

<?php $getdata->main("3") ?>
<!-- more html...-->
</HTML>
--------------------
...etc...

The getdata class is similar to the following...

Code:
<?php
class getdata {
          // Declare variables
          function getdata (){
          //open link to the database
          }
          function main($db_id){
          //get data using $db_id as a filter and echo it
          echo "Here is your data ".$yourdata;
          } 

}
?>

However, the link is lost whenever the function is called, even though the database connection has not yet been closed, and the object is still considered alive. The only way it works is if I include the connection code in the actual called function.... but as I said this means a new connection to the db every time.

I have tried various methods of keeping the connection alive... any suggestions welcome :) and appreciated :-D

Sorry for long post, found this a bit difficult to explain :-/
 
Insufficient data for a meaningful answer.


Without more explicit information on the internal workings of your class, it is impossible to anything better than the most general of advice.


The PHP online manual entry for mysql_connect() states, "If a second call is made to mysql_connect() with the same arguments, no new link will be established".

Althought the PHP online manual entry for mysqli_connect() doesn't say so, it has been my experience that this function behaves the same way.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for your quick response :)

I was trying to simplify the post to keep it shorter, however, I see what you mean it is difficult to see what I am on about.

I have tried using mysql_connect() in the contructor with the logic that it will keep the connection open for that object and any subsiqent calls to the object.

$db_x = mysql_connect ($db_info[host], $db_info[username], $db_info[password]) or die (mysql_error());

When I run a query on $db_x in a subsiquent class function call (after the object is created) the connection is no longer there. I have even tried declaring $db_x as a class variable, but the connection is still lost. (using $this->db_x)

I have also tried using mysql_pconnect() although some do not recommend this, but it still did not work.

I have come across this "If a second call is made to mysql_connect() with the same arguments, no new link will be established", before but it seemed to me that in a class situation new links were being established... it behaved like the arguments were being lost.

Does this explain it a little clearer? :)
 
I still don't know what the exact problem is, but this line:

$db_x = mysql_connect ($db_info[host], $db_info[username], $db_info[password]) or die (mysql_error());

seems indicative. Were I doing this, I would write the code such that it explicitly stores and explicitely uses the database connection handle:

Code:
<?php

class foo 
{
	var $db_connector;
	
	function foo ()
	{
		$this->db_connector = mysql_connect('localhost', 'test', 'test');
		myql_select_db ('test', $this->db_connector);
	}
	
	function fetch_one_row()
	{
		$query = "SELECT * from foo order by rand() limit 1";
		
		print $this->db_connector;
		
		$rh = mysql_query ($query, $this->db_connector) or die (mysql_error($this->db_connector));
		
		return (mysql_fetch_assoc($rh));
	}
}

$bar = new foo();

print_r ($bar->fetch_one_row());

print_r ($bar->fetch_one_row());

?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thankyou!!!

OK, I have not explained my problem very well, but you have managed to understand me enough... as you have helped me resolve it!

I used your much more simiplified code (unlike my poor attempt at simplification! :~/) and used it to experiment.

Your code was pretty much what I had attempted to do, only I was including a db configuration file which contained the connectivity code. For some reason that I cant fathom now, whatever I had attempted did not work. It just would not pass across the connection link variable... but it does now %-)

This was something I tried to do some time ago in my current project but had given up due to higher priorties... well, I had also gotten fed up with it. I have since lost the original code so I cant say exactly what I was doing wrong... but this re-write has worked anyways thanks for your help :) a shiny star for you [medal][thumbsup].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top