Results 1 to 4 of 4

Thread: Can you use a variable in a WHERE clause.

  1. #1
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can you use a variable in a WHERE clause.

    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:

    Code:
    $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
    Last edited by Snookerman; 08-16-2009 at 05:16 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try this:
    PHP Code:
    $query="SELECT webSite, userName, password FROM my_sites WHERE webSite = '$site'"
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Try this:
    PHP Code:
    $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.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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"
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •