thomashw Posted September 21, 2010 Share Posted September 21, 2010 What's the correct syntax for having a variable in a select statement? Here's an example of what I'm trying to do (after I'm already connected to the database). $username = "thomas"; $query = mysql_query( "SELECT * from users WHERE username = $thomas" ); if( mysql_num_rows( $query ) > 0 ) { bla bla... It works if I don't put the WHERE part in, but I get an error if I use it, so I'm assuming I have the wrong syntax for using a variable in the select statement. Link to comment https://forums.phpfreaks.com/topic/214031-syntax-for-variable-in-select-statement/ Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 $username = "thomas"; $query = mysql_query( "SELECT * FROM `users` WHERE `username` = '".$username."' " ); if( mysql_num_rows( $query ) > 0 ) { You were referencing the wrong variable name! This of course assumes that your asking the sql server the correct questions and that the column names & table names are correct & that you have a valid connection to the DB.. Rw Link to comment https://forums.phpfreaks.com/topic/214031-syntax-for-variable-in-select-statement/#findComment-1113825 Share on other sites More sharing options...
thomashw Posted September 21, 2010 Author Share Posted September 21, 2010 Oh, haha, no I wasn't. I just mistyped it up there. Anyway, thank you. My syntax was a little off. Link to comment https://forums.phpfreaks.com/topic/214031-syntax-for-variable-in-select-statement/#findComment-1113839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.