Reuse function effectively

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pakcik_Kantin
Forum Newbie
Posts: 19
Joined: Mon Jan 13, 2003 8:23 pm
Location: Kuala Lumpur

Reuse function effectively

Post by Pakcik_Kantin »

I have a lot of functions , actually to cut down my script and programming.
Sample.

##########################
<?
include('mydb.inc.dat');
include('myprofile.inc.dat');
include('myclaims.inc.dat');
include('myleave.inc.dat');
?>
<html>
<title>User profile</title>

Name :<? username()?>
Age :<? userage()?>
LeaveBalance :<? LeaveBalance()?>
TimeoffBalance :<? TimeoffBalance()?>
Claims Balance :<? ClaimsBalance()?>
</html>
##############################
Each of these function, open connection to database. Is it a good practise !.

I dont like to dumb all my sql code in 1 php file, like people use Macromedia Dreamwever MX. I use Crimson Editor that is why i code my program like this.

TQ
crazyjimsmith
Forum Commoner
Posts: 28
Joined: Sat Jan 11, 2003 1:46 pm
Location: Sydney
Contact:

Post by crazyjimsmith »

Is crimson open source? If so where can I get it from?
Pakcik_Kantin
Forum Newbie
Posts: 19
Joined: Mon Jan 13, 2003 8:23 pm
Location: Kuala Lumpur

Crimson Editor is the best for php coder

Post by Pakcik_Kantin »

You can find it at

http://download.com.com and try to find crimson editor.

It is actually a binary editor, so u can use it for C,C++,JAVA,JSP,ASP and others...

The best tool ever
crazyjimsmith
Forum Commoner
Posts: 28
Joined: Sat Jan 11, 2003 1:46 pm
Location: Sydney
Contact:

Post by crazyjimsmith »

Thanks for the link.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Crazy, here's how I prefer to do it. It allows a lot of flexibility in how the db connection is made, prevents repeatedly connecting to the db, and it's pretty simple:

Code: Select all

##########################
<?
include('mydb.inc.dat');
include('myprofile.inc.dat');
include('myclaims.inc.dat');
include('myleave.inc.dat');

$conn = db_connect();
?>
<html>
<title>User profile</title>

Name :<? username($conn)?>
Age :<? userage($conn)?>
LeaveBalance :<? LeaveBalance($conn)?>
TimeoffBalance :<? TimeoffBalance($conn)?>
Claims Balance :<? ClaimsBalance($conn)?>
</html>
##############################
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

As long as each function that needs to connect to a database opens AND closes the connection there isn't a problem.
crazyjimsmith
Forum Commoner
Posts: 28
Joined: Sat Jan 11, 2003 1:46 pm
Location: Sydney
Contact:

database script

Post by crazyjimsmith »

I am going to try to write my first database script soon. I have the front part almost completed and I am looking at desing the database now.

It is for a hypothetical theatre where the seats get reserved.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

hmmm....

Post by llimllib »

Crazy, looking at your script a little further, why use more than one query? Why not:

Code: Select all

<?php

##########################
<?
include('mydb.inc.dat');
include('myprofile.inc.dat');
include('myclaims.inc.dat');
include('myleave.inc.dat');

$conn = db_connect();
$row = getUserValues($conn);
?>
<html>
<title>User profile</title>

Name :<? print $row[0]?>
Age :<? print $row[1]?>
LeaveBalance :<? print $row[2]?>
TimeoffBalance :<? print $row[3]?>
Claims Balance :<? print $row[4]?>
</html>

<?php
function getUserValues($conn)
{
$query = "
    SELECT user_name
               , user_age
               , leave_balance
               , time_off_balance
               , claims_balance
    FROM table
    WHERE user_id = $_SESSION['user_id']";
$result = db_query($result, $conn);
$row = db_fetchRow($result);
return $row;
}
?>
##############################
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Gen-ik wrote: As long as each function that needs to connect to a database opens AND closes the connection there isn't a problem.
I'm not trying to jump on you or anything. Please don't take it that way. :wink: I've made my share of mistakes and even had 'em published. 8O Now I've got my own blog site so that I can say all kinds of way out stuff. :twisted:

Anyways, it would actually be better to open only once connection to a server that lasts for the duration of the script. Opening a db connection is a bit of an expensive operation. Better to do it only once. In case you didn't know, one can run multiple queries through one connection.

Cheers,
BDKR
Pakcik_Kantin
Forum Newbie
Posts: 19
Joined: Mon Jan 13, 2003 8:23 pm
Location: Kuala Lumpur

The real situation

Post by Pakcik_Kantin »

i have 3 system.
1. Employee staff
2. Claims system
3. Leave System

Each of these database have their own tables definiton.

To grab data from these 3 databases , i simply create simple for easy debugging. When i benchmark it, only .2 to 1 second slower, than using JOIN statement. Therefore i can use all of these functions anytime

Sample

#####For connection
function shar_dbConnect()
{

global $shar_config;
global $shar_db_link;

if ($shar_config['persistent_connections'])
$shar_db_link = @mysql_pconnect ($shar_config['dbhost'], $shar_config['dbuser'], $shar_config['dbpassword']);
else
$shar_db_link = @mysql_connect ($shar_config['dbhost'], $shar_config['dbuser'], $shar_config['dbpassword']);

if ($shar_config['compatibility_mode'])
return $shar_db_link;

if (@mysql_select_db ($shar_config['dbname'], $shar_db_link))
return $shar_db_link;

}

function shar_dbQuery($query)
{
global $shar_last_query;
global $shar_db_link;

$shar_last_query = $query;
return @mysql_query ($query, $shar_db_link);
}

####When inserting
function req_pAinsert()
{
global $shar_var;

$shar_db_link = shar_dbConnect();

$sql=" INSERT INTO req_detail
VALUES (
NULL,
'".$shar_var['partA_reqName']."',
'".$shar_var['partA_reqExt']."',
'".$shar_var['partA_reqDept']."',
'".$shar_var['partA_reqApprovedHOD']."',
'".$shar_var['partA_reqApprovedHODBy']."',
now()
)
";

$result=shar_dbQuery($sql);
}

##When viewing
function req_pBcrdView()
{
global $shar_var;

$shar_db_link = shar_dbConnect();

$sql="SELECT * FROM
list_crd WHERE uid=".$shar_var['post_id']."
ORDER BY crd_name ASC
";

$result =shar_dbQuery($sql);
}
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

"I'm not trying to jump on you or anything." - DBKR
I quickly took a sidestep before you landed on me :)

Opening just one database connection for the entire script is the best way to do it I agree. I was simply saying that it doesn't do any harm to have more then one connection open at a time.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Packick,

just a thought, because I have a similar method for doing database connections, but I think you should return errors so that you can more specifically tell what goes wrong when something does. For example, my db_query function looks like this:

Code: Select all

<?php
function db_query($query)
{
    global $connection;

    $result = @mysql_query($query, $connection) 
        or $result['error'] = mysql_error();

    return $result
}
?>
and then, when I call it:

Code: Select all

<?php
$result = db_query($query);
if(isset($result['error'])) { printErrorPage($result['error']); }
?>
Post Reply