PHP / MySQL SELECT problem

stevet70

New Member
Messages
35
Reaction score
0
Points
0
I'm attempting to set up a simple MySQL SELECT which uses a variable to identify the row it selects.

If I use the following (using an actual id rather than a variable) it works perfectly:

<?php
// Request text for page
$content = @mysql_query('SELECT text FROM page_text WHERE id="9"');
if (!$content) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
// Display text for page
while ($row = mysql_fetch_array($content)) {
echo $row['text'];
}
?>

However as soon as I try to introduce a variable such as:

<?php $id = 9; ?>

<?php
// Request text for page
$content = @mysql_query('SELECT text FROM page_text WHERE id="$id"');
if (!$content) {
...

it doesn't work

I've tried a few different options with " and ' changes, but I either get nothing or an error message

The plan is to use the SELECT statement as an Include, hence wanting to set up the variable.

Any thoughts?
Thanks
 

manik

Member
Messages
49
Reaction score
0
Points
6
Why not try to print what sql gets generated and see for yourself where things have gone wrong.
I presume that you are using some text box for that variable. There may be blank spaces with it. Trim it out and see for yourself. but the best way to debug is to insert print statements.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
The string must be enclosed in double-quotes for variables to be evaluated. So change it to:

PHP:
$content = @mysql_query("SELECT text FROM page_text WHERE id=\"$id\"");
 
Top