Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
query function advice
Old 08-02-2010, 10:34 PM query function advice
Extreme Talker

Posts: 173
Trades: 0
I am looking for some experienced advice when it comes to functions and query statements.

I am writing code that I am constantly having to query a db, does it make sense to create a function that I call every time and return result or is it better to just make the calls in the main body of the code. It just seems that I am forever just typing the same six or so lines over and over.

I have attached a sample of what I currently have and a proposed function. I know the idea of functions it reuse what you have already done but I am not sure if this also applies to db queries as it may cause confusion??

I guess part of my question also lies in the fact that I really do not understand what is being returned in the result, is is a pointer to the db or an array of values. In which case, is it better to open the db, query and close the db all in a very neat package.

Looking for some worldly advice?

Quote:
<?php
// start session
session_start();
// set up some global variables
@include 'salutations.php';
@include 'globalcfg.php';
@include 'db_connect.php';
$sql='SELECT quote, image, quoted FROM tbl_list_quotes ORDER BY RAND() LIMIT 1';
$result = mysql_query($sql, $link);
if (!$result) {
echo 'DB Error, could not query the database MySQL Error: '.mysql_error();
exit;
}
$row = mysql_fetch_assoc($result);
$quote=$row['quote'].'<br /><br />-- '.$row['quoted'].' --';
echo $_GET["project_id"];
if(isset($_GET["project_id"])) $_SESSION["project_id"]=$_GET["project_id"];
if(isset($_GET["tab"])) $_SESSION["tab"]=$_GET["tab"];
if(empty($_SESSION["tab"])) $_SESSION["tab"]=2;
echo $_SESSION["tab"].'PID'.$_SESSION["project_id"];
?>
or should I just call a function like

Quote:
function query($sql){
@include 'globalcfg.php';
@include 'db_connect.php';
$result=mysql_query($sql, $link);
if (!$result) {
echo 'DB Error, could not query the database MySQL Error: '.mysql_error();
exit;
}
return $result;
// dispose of result set
mysql_free_result($result);
// close connection to database server
mysql_close($link);
}

Last edited by dgkindy; 08-02-2010 at 10:36 PM.. Reason: grammatical error
dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-02-2010, 10:54 PM Re: query function advice
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If you find you are doing things over and over again writing a function is probably a good idea.

Looking at your proposed function, however, I see some potential problems: If you plan on calling your query function multiple times in a single execution you'll want to move your includes outside of the function (or if you must, use include_once instead of include). The same thing applies to connecting to and closing your database connection; if you're calling query more than once it is wasteful to connect and close your connection every time it is called.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 08-03-2010, 06:11 AM Re: query function advice
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
Its always better to have Database related function (ie for CURD operation) in a separate file and as functions. Respective functions shall be called where ever necessary.

The benefits are
1. Its very easy to debug , when ever something goes wrong.

2. The code will be neat
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Old 08-03-2010, 07:22 AM Re: query function advice
Extreme Talker

Posts: 173
Trades: 0
Thanks for your advice.

A question regarding the includes, what I found was that the function did not seem to be aware of the db connection so I moved it inside of the function, is this something that needs to be passed to the function or should it be available globally?
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 08-03-2010, 07:39 AM Re: query function advice
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
You need to declare your variable as 'global' inside your function..

For example, if $db is your database connection object..then inside your function..

function functionname
{
global $db;

// your function definition here
}

and need to include the file inside the function, also its not a good practice to do this, since PHP will include the file every time you call the function.

Instead if you include it outside, it will be included once..
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to query function advice
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22397 seconds with 12 queries