First off, Sorry if this has already been asked.
I looked around but couldn't find any answers for it, Or maybe I was searching using the wrong words.
I have a long SQL query that I need to execute using PHP. It requires a large number of variables to be updated.
This is what I mean:
$user = json_decode($stringWithJson);
$reallyLongSqlQuery = "UPDATE `profile` SET `userid` = '{$user->userid}', `name` = '{$user->username}', `lastlogoff` = '{$user->userlastlogoff}', `profileurl` = '{$user->userprofileurl}', `avatar` = '{$user->useravatar}', `avatarmedium` = '{$user->useravatarmedium}', `useravatarfull` = '{$user->useravatarfull}', `state` = '{$user->userprofilestate}', `realname` = '{$user->userrealname}', `timecreated` = '{$user->userprofilecreatedunix}' WHERE `id` = 1;";
mysql_query($reallyLongSqlQuery);
This works fine and all, but It's a lot of code for a single line. Is there any way I can tidy
this up?
Example:
$reallyLongSqlQuery = "UPDATE `profile` SET `userid` = '" . $user->userid .
"', `name` = '" . $user->username .
"', `lastlogoff` = '" . $user->userlastlogoff .
"', `profileurl` = '" . $user->userprofileurl .
"', `avatar` = '" . $user->useravatar .
"', `avatarmedium` = '" . $user->useravatarmedium .
"', `useravatarfull` = '" . $user->useravatarfull .
"', `state` = '" . $user->userprofilestate .
"', `realname` = '" . $user->userrealname .
"', `timecreated` = '" . $user->userprofilecreatedunix .
"' WHERE `id` = 1;";
This doesn't fly off the screen in one giant line, but it looks even messier in my opinion.
Another way I've though of is predefining all the variables beforehand, Like so:
$userid = $user->userid;
$username = $user->username;
$userlastlogoff = $user->userlastlogoff;
$userprofileurl = $user->userprofileurl;
$useravatar = $user->useravatar;
$useravatarmedium = $user->useravatarmedium;
$useravatarfull = $user->useravatarfull;
$userprofilestate = $user->userprofilestate;
$userrealname = $user->userrealname;
$userprofilecreatedunix = $user->userprofilecreatedunix;
$reallyLongSqlQuery = "UPDATE `profile` SET `userid` = '{$userid}', `name` = '{$username}', `lastlogoff` = '{$userlastlogoff}', `profileurl` = '{$userprofileurl}', `avatar` = '{$useravatar}', `avatarmedium` = '{$useravatarmedium}', `useravatarfull` = '{$useravatarfull}', `state` = '{$userprofilestate}', `realname` = '{$userrealname}', `timecreated` = '{$userprofilecreatedunix}' WHERE `id` = 1;";
Once again, This works fine but there must be an easier (and tidier) way to do it.
Anyone have a solution?