Kanuski2 Posted October 4, 2021 Share Posted October 4, 2021 I did some php coding a few years ago and am trying to start again but the rules have changed. I did this query: $sql = "SELECT * FROM book WHERE solution='HIN ORDER BY id"; It works great. Then $item="HIN"; $sql = "SELECT * FROM book WHERE solution=$item ORDER BY id"; and I get no results. What gives? Quote Link to comment https://forums.phpfreaks.com/topic/313864-variable-in-query/ Share on other sites More sharing options...
Kanuski2 Posted October 4, 2021 Author Share Posted October 4, 2021 Forgot an apostrophe, solution='HIN' Quote Link to comment https://forums.phpfreaks.com/topic/313864-variable-in-query/#findComment-1590670 Share on other sites More sharing options...
Barand Posted October 5, 2021 Share Posted October 5, 2021 (edited) $item still requires the quotes around it, just like HIN did. PS Instead of putting variables in the query, you should use prepared queries with placeholders for the values $item = "HIN"; $stmt = $pdo->prepare("SELECT * FROM book WHERE solution=? ORDER BY id"); $stmt->execute( [ $item ] ); Edited October 5, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313864-variable-in-query/#findComment-1590675 Share on other sites More sharing options...
Kanuski2 Posted October 6, 2021 Author Share Posted October 6, 2021 Awesome. Greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/313864-variable-in-query/#findComment-1590730 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.