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. Quote Link to comment 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'"; Quote Link to comment 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' Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
182x Posted July 22, 2007 Author Share Posted July 22, 2007 Thank you 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.