ElectronikBean
Programmer
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
--------------
--------------------
...etc...
The getdata class is similar to the following...
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 :-/
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 :-/