Page 1 of 1

shortcuts good or bad?

Posted: Sun Mar 11, 2007 1:16 am
by psychotomus
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());
}

Posted: Sun Mar 11, 2007 1:41 am
by John Cartwright
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());
}

Posted: Sun Mar 11, 2007 3:35 am
by volka
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)

Posted: Sun Mar 11, 2007 3:43 am
by psychotomus
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 ;]