Log in

View Full Version : Resolved Can you use a variable in a WHERE clause.



Columbo
08-16-2009, 02:20 AM
I need to find a way to use a variable in a WHERE clause.
I have a page with a form that a user inputs a web site name. (for example mysite.com). No http:// or www.

That input is passed to another page using $_POST['domain'];

The other page assigns the user input into a variable called $site by using:



$site=$_POST['domain'];

$query="SELECT webSite, userName, password FROM my_sites WHERE webSite=$site";

$result=mysql_query($query, $db_handle) or die(mysql_error());



...but it flags an error:

Warning: mysql_query() expects parameter 2 to be resource, string given in C:\wamp\www\logins\dosearch.php on line 12

If I type in the name of the web site instead of using the variable $site, it works. It doesn't seem to like the variable. Can anyone tell me if a variable can be used and if so, what would be the syntax?

Thanks

Nile
08-16-2009, 02:24 AM
Try this:


$query="SELECT webSite, userName, password FROM my_sites WHERE webSite = '$site'";

Columbo
08-16-2009, 02:52 AM
Try this:


$query="SELECT webSite, userName, password FROM my_sites WHERE webSite = '$site'";

That works! I tried it before but used the double quote rather than the single quote. Didn't work.

Thank you very much for your help. Very much appreciated.

Nile
08-16-2009, 03:17 AM
Glad to help you! Your welcome!

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"