Tuesday, April 9, 2013

issue on inserting text or varchar values on mysql

when we insert text or varchar values into tables on mysql, we usually use the following format

mysql_query("INSERT INTO user (userid, name, gender) VALUES ('$user_id', '$user_name', '$user_gender'");

instead of

mysql_query("INSERT INTO user (userid, name, gender) VALUES ($user_id, $user_name, $user_gender");


yes, the difference is '$user_name' and  $user_name.

the advantage of former is that:
if your name is zhiguang cao (yes, there is one blank space between given name and surname )

when you insert  $user_name, it will be considered as two items: zhiguang  and cao respectively, and thus the result would not be correct generally.

while if you insert '$user_name', it will be considered as one item: 'zhiguang cao' would be one item instead of two.

I took me hours to find this problem although it seems not a big deal. thanks chenbo's help!

No comments:

Post a Comment