Jump to content

Syntax for variable in SELECT statement


thomashw

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
https://forums.phpfreaks.com/topic/214031-syntax-for-variable-in-select-statement/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.