182x Posted July 22, 2007 Share Posted July 22, 2007 Hey guys, When creating queries in PHP for example: $test="SELECT test FROM test WHERE testId = $testId"; Should there be single quotes round any part of the query? It works fine as it is, I just want to be sure I am writing my code to the proper standard. Thanks. Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/ Share on other sites More sharing options...
pocobueno1388 Posted July 22, 2007 Share Posted July 22, 2007 Here is a more proper way. $test = "SELECT `test` FROM `test` WHERE `testId` = '$testId'"; Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/#findComment-304799 Share on other sites More sharing options...
trq Posted July 22, 2007 Share Posted July 22, 2007 Non integer type values should be surrounded in quotes. So for example, if fld was of type VARCHAR your query might look like... SELECT fld FROM tbl WHERE fld = 'foo' Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/#findComment-304802 Share on other sites More sharing options...
trq Posted July 22, 2007 Share Posted July 22, 2007 Here is a more proper way. $test = "SELECT `test` FROM `test` WHERE `testId` = '$testId'"; Backticks should only be used to escape reserved words. There is simply no need for them otherwise. Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/#findComment-304803 Share on other sites More sharing options...
182x Posted July 22, 2007 Author Share Posted July 22, 2007 What if a field is of type VARCHAR but the value entered into it is a integer does it need the quotes? Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/#findComment-304805 Share on other sites More sharing options...
trq Posted July 22, 2007 Share Posted July 22, 2007 What if a field is of type VARCHAR but the value entered into it is a integer does it need the quotes? Yes. Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/#findComment-304813 Share on other sites More sharing options...
182x Posted July 22, 2007 Author Share Posted July 22, 2007 Thank you Link to comment https://forums.phpfreaks.com/topic/61258-solved-php-query-question/#findComment-304816 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.