Variable in where clause

$query="select id from myTable1 where title='[COLOR="Red"]title1[/COLOR]' " ;

The code above works fine.

when I change “where clause” from the code above like the code below, I met
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in dbAction1.php .

$query="select id from myTable1 where title=[COLOR="red"]$myVar[/COLOR] " ;

How can I put a variale in where clause without Warning?

$query="select id from myTable1 where title=[COLOR="Red"]'[/COLOR]$myVar[COLOR="Red"]'[/COLOR] " ;

It’s always a good idea to echo a query, to see what it looks like.
You had removed the apostophes around the value.

ok, some quick and simple debugging.

add the red code and that will display the actual sql query that will be run and hopefully the error in your line of code will become obvious.

 
$query="select id from myTable1 where title=$myVar " ;
 
[COLOR=red]echo $query;[/COLOR]
[COLOR=red] [/COLOR]
[COLOR=red]die();[/COLOR]

if you still have a problem, post back the output you get from

 
[COLOR=red]echo $query;[/COLOR]

Assuming you’re using mysql_*, remember to always escape any dynamic elements of your query.


<?php
$query = sprintf(
  "SELECT id FROM table WHERE name = '%s';",
  mysql_real_escape_string($var)
);
?>

Thank you, it works fine.

What about session variable in where clause?
The following code seems not to work correctly.

[b]code1[/b]
$query="select id from myTable1 where title='$_SESSION([COLOR="Red"]"[/COLOR]$myVar[COLOR="red"]"[/COLOR])' " ;

[b]result1[/b]
Parse error: syntax error, unexpected T_VARIABLE 

[b]code2[/b]
$query="select id from myTable1 where title='$_SESSION([COLOR="Red"]'[/COLOR]$myVar[COLOR="red"]'[/COLOR])' " ;

[b]result2[/b]
// It doesn't call the value of the id column.

Maybe here or [URL=“http://www.php.net/manual/en/language.types.array.php”]here would be better suited for you, a quick brush up on a few fundamentals is never wasted time. :wink:

Thank you, it’s good tools for confirmation.

I’ll check the code above with your code “echo $query.”