/    Sign up×
Community /Pin to ProfileBookmark

is this a safe function for building a sql query?

Hi,
On a page I’m building I have several lists built by pulling in the data from a mysql db. These lists are practically all the same, only the table names really change.
Basically rather than having the same blocks of code replicated everywhere I’ve put together a function to cut down on my code. Several parameters can be passed into the function to specify the fields I want to pull in, whether the results are sorted and any limit I may want to put in place. Does this look safe?

[code=php]function buildBlockList($all ,$tableName, $sort, $limit=””){

$sql = sprintf(“SELECT
%s
FROM
%s
%s
%s”,
mysql_real_escape_string($all),
mysql_real_escape_string($tableName),
mysql_real_escape_string($sort),
mysql_real_escape_string($limit));

$result = mysql_query($sql);

if( !$result ) {
echo “Error retrieving list data”;
}
while ($resultBlockList = mysql_fetch_array($result)) {

$resultBlockListlink_int_title[] = $resultBlockList[‘link_int_title’];
$resultBlockListlink_int[] = $resultBlockList[‘link_int’];

}
$BlockListlink_int_titleSize = count($resultBlockListlink_int_title);

?>

<?php

for ($j = 0; $j< $BlockListlink_int_titleSize ; ++$j) {
echo ‘<li><a href=”‘.$resultBlockListlink_int[$j].'”/>’.$resultBlockListlink_int_title[$j].'</a></li>’;
}

}[/code]

Here an example calling the function:

[code=php]buildBlockList(“link_int_title, link_int”, “betting_exchange”, “ORDER BY click_count DESC”, “LIMIT 0, 5”);[/code]

Thanks for any input!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderNov 07.2008 — Why go to all that trouble when mysql[B]i[/B] does it all for you?
×

Success!

Help @sickofitall_uk 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.18,
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,
)...