Welcome to WebmasterWorld Guest from 54.156.92.140

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Functions which use a database

Most efficient way of doing it

     
12:42 am on Jul 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:July 5, 2003
posts:167
votes: 0


Hi,

I have build myself a functions include to store all my most used functions. All of these functions access the mysql database. What is the best way to pass the variables needed to access the database correctly into each function?

In the main php I have:
include ("init.inc");
include ("functions.inc");

But of course in order to access the database each function must also have the include ("init.inc"); inside it. This I would imagine is inefficient because the init.inc itself has other includes inside it.

So should I pass the variables for the database into the function as arguments, or perhaps create another function in the functions.inc which returns the database variables as an array. Or is there a simpler way of doing things?

Any advice would be greatly appreciated.

Cheers

1:35 am on July 22, 2005 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8490
votes: 224



But of course in order to access the database each function must also have the include ("init.inc"); inside it.

I'm not sure I follow. If you have a file that gets included in every page (and you could use an auto_prepend file), use that to connect to the database. From that point on, the connection is available.

If I want a specific connection and DB resources link to be shared, usually I make the resource link a class member, but that's not at all necessary.

1:57 am on July 22, 2005 (gmt 0)

Junior Member

10+ Year Member

joined:July 5, 2003
posts:167
votes: 0


Sorry perhaps the example of one of my functions will help explain what I mean a bit better:

function costPerClick($aId, $perClickPayout, $fromDate, $toDate) {

include ("init-vg.inc");
$total = 0;

$db = mysql_connect($vgMyHost, $vgMyUser, $vgMyPass);
mysql_select_db($vgMyDB,$db);
$sql = "SELECT a_id FROM $vgTAHits WHERE ((a_id = $aId) AND (thetimestamp >= $fromDate) AND (thetimestamp < $toDate));";
$result = mysql_query($sql, $db) or die(mysql_error());
$noOfClicks = mysql_num_rows($result);
$total = ($noOfClicks * $perClickPayout);

return $total;

mysql_close($db);
}

All the database variables are inside the init-vg.inc .It seems that I need that include inside every function, or it does not work. Or am I missing something simple?

3:14 am on July 22, 2005 (gmt 0)

Full Member

10+ Year Member

joined:July 22, 2005
posts:202
votes: 0


You can include the file once and then specify the variables necessary as globals in the function.

For example, instead of putting the include for the database variables in every function, you'd add the following line:

global $vgMyHost, $vgMyUser, $vgMyPass, $vgMyDB;

The global keyword doesn't have a limit on how many variables can be listed. In fact if you were to want a function to have every global variable that's been declared available, you could use the following code:

foreach($GLOBALS as $var => $val)
$$var = $val;

4:18 pm on July 22, 2005 (gmt 0)

Moderator

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 25, 2002
posts:8490
votes: 224



You can include the file once and then specify the variables necessary as globals in the function.

The traditional (i.e. the old "C/C++") way, and probably better, would be to use constants. I have to say that I almost never use any globals except the pre-defined $_POST, _$GET and $_SESSION superglobal.

define('DB_HOST', ''vghost');

et cetera. Thes values are then available at any point, anywhere after being defined and, since constants can't be redefined, they are more hacker-safe than variables.