0

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 Answers 2

4

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
  );
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.