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. Quote Link to comment 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 Quote Link to comment 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. 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.