/    Sign up×
Community /Pin to ProfileBookmark

SQL Function doesn’t work twice

Hi all,

I’ve written a SQL function for use in my PHP pages, as follows:

[code=php]
function sql($query)
{
include(“config.common.php”);
global $dbName;
@mysql_select_db($dbName) or die(“<h1>Database Error</h1>Unable to connect to database”);
$sql = “” . $query . “”;
$result = mysql_query($sql);
define(“result”,$result);
unset($sql,$query);
}
[/code]

for use in pages as follows:

[code=php]sql(“SELECT * FROM table”);
while($row = mysql_fetch_array(result))
{

}
[/code]

My issue is this. If I try and use it twice in a page, then the first query works but the second does not. I cannot work out why this is happening?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 11.2010 — You cannot define() a constant twice. I would recommend that you [b]return[/b] the result so that the calling code can assign the result to a variable and use that, instead of the constant.

A couple other changes I would recommend is to pass the database name as a function parameter instead of using [b]global[/b] (which is generally a bad practice as it closely couples the function to the rest of the script), and do an include_once() (or maybe better require_once()) instead of the include(), so that you only include it if it has not already been included. Lastly, there is no need to concatenate empty strings to either end of the $query variable. In fact, there's not reason to assign it to another variable.
[code=php]
function sql($query, $dbName = null)
{
require_once ("config.common.php");
if (!empty($dbName)) {
@mysql_select_db($dbName) or die("<h1>Database Error</h1>Unable to connect to database");
}
$result = mysql_query($query);
if ($result == false) {
error_log(mysql_error() . "n$query");
}
return $result;
}

// USAGE:
$result = sql("SELECT * FROM table_name", "my_db");
if($result) {
while($row = mysql_fetch_assoc($result)) {
// do interesting stuff with the data
}
}
else {
// a nice error message for the user
}
[/code]

(PS: There's no need to unset() function variables, as they are automatically cleared once the function completes executing.)
Copy linkTweet thisAlerts:
@ralphchadkirkauthorDec 11.2010 — Thanks very much NogDog - works perfectly!
×

Success!

Help @ralphchadkirk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...