2

I am trying to print out some topic information, but it is not going so well. This is my query:

SELECT * FROM topics WHERE id='$read'

This doesn't work. I've echo'ed the $read variable, it says 1. So then if I do like this:

SELECT * FROM topics WHERE id='1'

It works perfectly. I don't get what is the problem. There's no hidden characters in $read or anything else like that.

4
  • When you say it doesn't work, what exactly isn't working? Are you getting an error? Is it not returning results? Commented Dec 27, 2012 at 16:46
  • 1
    What's the error message? Commented Dec 27, 2012 at 16:47
  • Do not echo variable for debugging, use var_dump. echo "1 "; will give you the same output as echo "1"; and you will be unaware of any non printable characters, while var_dump will tell you type and size in bytes. Commented Dec 27, 2012 at 17:12
  • @Jrod It just doesnt show anything, when i do mysql_num_rows it says 0 Commented Dec 27, 2012 at 17:14

7 Answers 7

1

Try like this:

$query = "SELECT * FROM topics WHERE id='" . $read . "'"
Sign up to request clarification or add additional context in comments.

Comments

0

ID is normally a numeric field, it should be

$id = 1;
$query = "SELECT * FROM topics1 WHERE id = {id}"

If you are using strings for some reason, fire a query like

$id = '1';
$query = "SELECT * FROM topics1 WHERE id = '{$id}'"

Comments

0
SELECT * FROM topics WHERE id=$read

it consider it as string if you put i single quotes

Comments

0

I wonder why all the participants didn't read the question that clearly says that query with quotes

SELECT * FROM topics WHERE id='1'

works all right.

As for the question itself, it's likely some typo. Probably in some other code, not directly connected to $read variable

Comments

0

try

$query = sprintf("SELECT * FROM topics WHERE id='%s';",$read);

Also remember to escape the variable if needed.

Comments

0

Looks like you might have an issue with the query generation as everyone else is pointing to as well. As Akash pointed out it's always good to build your query in to a string first and then feed that string to the MySQL API. This gives you easy access to handy debugging techniques. If you are still having problems try this.

$id = 1;
$query = "SELECT * FROM `topics1` WHERE `id`={$id}";
echo ": Attempting Query -> {$query}<br />";

$res = mysql_query($query, $dblink);
if($res <= 0)
    die("The query failed!<br />" . mysql_error($dblink) . "<br />");

$cnt = mysql_num_rows($res);
if($cnt <= 0)
{
    $query = "SELECT `id` FROM `topics1`";
    echo "No records where found?  Make sure this id exists...<br />{$query}<br /><br />";

    $res = mysql_query($query, $dblink);
    if($res <= 0)
        die("The id listing query failed!<br />" . mysql_error($dblink) . "<br />");

    while($row = mysql_fetch_assoc($res))
        echo "ID: " . $row['id'] . "<br />";
}

This will at least let you monitor between calls, see what your query actually looks like, what mysql says about it and if all else fails make sure that the ID you are looking for actually exists.

Comments

-1

try with this : SELECT * FROM topics WHERE id=$read

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.