SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot bensheard's Avatar
    Join Date
    Jun 2006
    Location
    Cape Town | South Africa
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using variables in a MySQL query - problem PLEASE HELP

    Hi
    I have been having a problem sending a MySQL query with a php variable in it. This is my code:
    Code:
    $sql = MYSQL_QUERY('SELECT `messages`.`msg_no`, `messages`.`name_for`, `messages`.`name_from`, `messages`.`date_left`, `messages`.`message`'
            . ' FROM messages'
            . ' WHERE (`messages`.`name_for` = $query_for)'
            . ' ORDER BY `messages`.`msg_no` DESC LIMIT 0, 30');
    Furhter down the script I have:
    Code:
    while ($row = mysql_fetch_array($sql, MYSQL_NUM))
    {
    echo "<TR>";
    echo "<TD width='13%'>", $row[2], "</TD><TD width='16%'>", $row[3], "</TD><TD width='71%' >", $row[4], "</TD>";
    echo "</TR>";
    On the 5th line of the first piece of code I have the var $query_for, which has a string value. If I use this code in place of that line:
    Code:
            . ' WHERE (`messages`.`name_for` = Ben)'
    It works perfectly but when using the variable there instead it says when I open the page:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    What is the problem??? Please help

  2. #2
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bensheard
    On the 5th line of the first piece of code I have the var $query_for, which has a string value. If I use this code in place of that line:
    Code:
            . ' WHERE (`messages`.`name_for` = Ben)'
    It works perfectly but when using the variable there instead it says when I open the page:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    What is the problem??? Please help
    You need to change your single quotes to double quotes:

    Code:
            . " WHERE ('name_for' = '$query_for')"


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
  •