hostfreak Posted November 19, 2007 Share Posted November 19, 2007 I know to use single quotes around values in a query (unless an integer). I'm not sure why though. I did know, but it became habit so I forgot why it actually should be done. I think it may have something to do with the escape sequences? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2007 Share Posted November 19, 2007 I do not think its because of the escape sequence maybe because its treated as a string same as php not too sure do will check it up Quote Link to comment Share on other sites More sharing options...
hostfreak Posted November 19, 2007 Author Share Posted November 19, 2007 Yeah, after posting this I ruled the escape sequences out. I've checked the mysql manual, but it's like trying to find a needle in a haystack. It probably does have something to do with the way mysql parses the values. Quote Link to comment Share on other sites More sharing options...
trq Posted November 19, 2007 Share Posted November 19, 2007 Values are generally strings (unless they are ints), string in sql need to be surrounded in quotes, just as they do in php. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted November 19, 2007 Author Share Posted November 19, 2007 Another thing that confuses me is that in php you can only parse a variable in double quotes unless you use the concatenation operator. However, if your doing a query (mysql_query) it will parse the variable surrounded by the single quotes. I am assuming it's only within the mysql_query function that php will parse the variable like this? Do to the fact that inside that function, the quotes will be irrelevant (being as they will then be parsed by mysql)? Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 19, 2007 Share Posted November 19, 2007 Another thing that confuses me is that in php you can only parse a variable in double quotes unless you use the concatenation operator. However, if your doing a query (mysql_query) it will parse the variable surrounded by the single quotes. I am assuming it's only within the mysql_query function that php will parse the variable like this? Do to the fact that inside that function, the quotes will be irrelevant (being as they will then be parsed by mysql)? Keep in mind that the PHP parser is interpreting the entire string before it gets sent to the MySQL query. So, when you have a variable within the single quotes, PHP only knows that it is part of a larger string within the double quotes. If you were to echo the SQL statement out to the screen, you would see what is actually sent to the MySQL parser: <?php $name = 'Obsidian'; $sql = "SELECT * FROM users WHERE username = '{$name}'"; echo $sql; // SELECT * FROM users WHERE username = 'Obsidian' ?> Quote Link to comment Share on other sites More sharing options...
hostfreak Posted November 19, 2007 Author Share Posted November 19, 2007 Makes perfect sense now. Thanks for the clarification thus far guys. Only one more question and I think I'll be good. As far as the values needing to be surrounded by quotes in mysql as well, how come it will not give off an error if a value isn't surrounded by quotes? Quote Link to comment Share on other sites More sharing options...
trq Posted November 19, 2007 Share Posted November 19, 2007 It will throw an error if the value is expected to be within quotes. Quote Link to comment Share on other sites More sharing options...
hostfreak Posted November 19, 2007 Author Share Posted November 19, 2007 Alright, got it. Once again, thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.