PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
psychotomus
Forum Contributor
Posts: 487 Joined: Fri Jul 11, 2003 1:59 am
Post
by psychotomus » Sun Mar 11, 2007 1:16 am
is writing code the short way like this for SQL good or bad? i mean, is it frowned upon?
Code: Select all
function update_user_downloads($username, $type)
{
$result = mysql_query("SELECT user_" . $type . "_downloads FROM users WHERE username='$username'") or die(mysql_error());
$download = mysql_fetch_object($result);
switch ($type)
{
case "anime":
$downloads = $download->user_anime_downloads + 1;
break;
case "manga":
$downloads = $download->user_manga_downloads + 1;
break;
case "midi":
$downloads = $download->user_midi_downloads + 1;
break;
case "mp3":
$downloads = $download->user_mp3_downloads + 1;
break;
case "software":
$downloads = $download->user_software_downloads + 1;
break;
}
mysql_query("UPDATE users
SET user_" . $type . "_downloads ='$downloads'
WHERE username='$username'") or die(mysql_error());
}
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Mar 11, 2007 1:41 am
I'm not quite sure what your asking. Please explain.
P.S. Your code could be shortened to simply
Code: Select all
function update_user_downloads($username, $type)
{
$sql = 'UPDATE `users` SET `user_'. $type .'_downloads` = (`user_'. $type .'_downloads` + 1) WHERE `username` = \''.$username.'\'';
mysql_query($sql) or die(mysql_error());
}
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Sun Mar 11, 2007 3:35 am
And instead of having one table for each download type you could use one big table and store the type in a field. Makes it a loot easier to pull data concerning different types (or where the type doesn't matter)
psychotomus
Forum Contributor
Posts: 487 Joined: Fri Jul 11, 2003 1:59 am
Post
by psychotomus » Sun Mar 11, 2007 3:43 am
Jcart wrote: I'm not quite sure what your asking. Please explain.
P.S. Your code could be shortened to simply
Code: Select all
function update_user_downloads($username, $type)
{
$sql = 'UPDATE `users` SET `user_'. $type .'_downloads` = (`user_'. $type .'_downloads` + 1) WHERE `username` = \''.$username.'\'';
mysql_query($sql) or die(mysql_error());
}
wow never knew you could just ad + 1 next to an interget field in sql and it would increment ;]