I am new to psu's posted answer and will definitely check this out, but from a quick read you have to be very careful when using these special syntaxes.
One reason that comes to mind: you do not know what can happen to the table into which you insert or update information. If several unique definitions are defined, then you may have serious problems, and this is common when scaling applications.
2 replacement for syntax is a functionality that rarely occurs in my applications. Since I do not want to lose data from colomns in a row that was already in the table.
I am not saying that his answer is erroneous, and only when indicating precautions when using it due to the above reasons and possible more.
as stated in the first article, I may be new to this, but at this very moment I prefer:
$result = mysql_query("select user_id from profile where user_id = $user_id limit 1"); if(mysql_num_rows($result) === 1){ //do update like you did } else{ /** * this next line is added after my comment, * you can now also leave the if(count()) part out, since the array will now alwayss * hold data and the query won't get invalid because of an empty array **/ $update_data_profile['user_id'] = $user_id; if(count($update_data_profile)){ $columns = array(); $values = array(); foreach($update_data_profile as $field => $data){ $columns[] = $field; $values[] = $data; } $sql = "insert into profile (" . implode(",", $columns) .") values ('" . implode("','", $values) . "')" ; var_dump($sql); //remove this line, this was only to show what the code was doing /**update**/ mysql_query($sql) or echo mysql_error(); } }
source share