homepage Welcome to WebmasterWorld Guest from 54.227.41.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Functions which use a database
Most efficient way of doing it
Aberdeen




msg:1296873
 12:42 am on Jul 22, 2005 (gmt 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

 

ergophobe




msg:1296874
 1:35 am on Jul 22, 2005 (gmt 0)


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.

Aberdeen




msg:1296875
 1:57 am on Jul 22, 2005 (gmt 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?

Mr_Fern




msg:1296876
 3:14 am on Jul 22, 2005 (gmt 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;

ergophobe




msg:1296877
 4:18 pm on Jul 22, 2005 (gmt 0)


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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved