Jump to content

Syntax for variable in SELECT statement


thomashw
 Share

Recommended Posts

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
Share on other sites

$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
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.