简体   繁体   中英

PHP / SQL Query and PHP Variables

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.

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
  );

You forgot the dollar sign and the single quotations:

AND id='$myVar'

Additionally, you may want to consider using heredoc :

$query = <<<MYSQL
SELECT *
FROM mytable
WHERE time>$time
AND id='$myVar'
ORDER BY id ASC
LIMIT $display_num
MYSQL;

$msgg = mysql_query($query, $myconn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM