Page 1 of 1
Reuse function effectively
Posted: Mon Jan 13, 2003 8:23 pm
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
Posted: Tue Jan 14, 2003 8:03 am
by crazyjimsmith
Is crimson open source? If so where can I get it from?
Crimson Editor is the best for php coder
Posted: Tue Jan 14, 2003 10:04 am
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
Posted: Tue Jan 14, 2003 1:22 pm
by crazyjimsmith
Thanks for the link.
Posted: Tue Jan 14, 2003 1:28 pm
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>
##############################
Posted: Tue Jan 14, 2003 1:30 pm
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.
database script
Posted: Tue Jan 14, 2003 1:42 pm
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.
hmmm....
Posted: Tue Jan 14, 2003 1:55 pm
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;
}
?>
##############################
Posted: Tue Jan 14, 2003 2:03 pm
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.

I've made my share of mistakes and even had 'em published.

Now I've got my own blog site so that I can say all kinds of way out stuff.
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
The real situation
Posted: Tue Jan 14, 2003 9:23 pm
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);
}
Posted: Wed Jan 15, 2003 7:45 am
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.
Posted: Wed Jan 15, 2003 11:36 am
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']); }
?>