douxing5598 2011-11-24 23:40
浏览 27
已采纳

PHP / SQL查询和PHP变量

I have this query below:

$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id='someid'
ORDER BY id ASC
LIMIT $display_num",$myconn);

see this line: AND id='someid' <-- someid ...

OK, the query above returns 2 results as expected...

Now for the problem....

-- I have a variable myVar and it's content is "someid" (without quotes)...same as the string 'someid'

When I do this:

$msgg = mysql_query("SELECT *
FROM mytable
WHERE time>$time
AND id=myVar
ORDER BY id ASC
LIMIT $display_num",$myconn);

See: myVar <-- this variable contans .. someid

The second query returns no results.

Update: When using ... AND id='$myVar' it sees $myVar as empty for some reason.

  • 写回答

2条回答 默认 最新

  • dongmu1390 2011-11-24 23:43
    关注

    Put a $ in front of myVar:

    $msgg = mysql_query(
    "SELECT *
       FROM mytable
      WHERE time > $time
        AND id   = '$myVar'
      ORDER BY id ASC
      LIMIT $display_num", $myconn
      );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?