Reuse function effectively
Moderator: General Moderators
-
Pakcik_Kantin
- Forum Newbie
- Posts: 19
- Joined: Mon Jan 13, 2003 8:23 pm
- Location: Kuala Lumpur
Reuse function effectively
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
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:
-
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
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
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:
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>
##############################-
crazyjimsmith
- Forum Commoner
- Posts: 28
- Joined: Sat Jan 11, 2003 1:46 pm
- Location: Sydney
- Contact:
database script
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.
It is for a hypothetical theatre where the seats get reserved.
hmmm....
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;
}
?>
##############################I'm not trying to jump on you or anything. Please don't take it that way.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.
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
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);
}
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);
}
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:
and then, when I call it:
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
}
?>Code: Select all
<?php
$result = db_query($query);
if(isset($result['error'])) { printErrorPage($result['error']); }
?>