PHP - MySQL WHERE无法正常工作

When I add this code to my Php file

    include "sql_connect.php";
    $query_blog="SELECT * FROM messages";
    $result_blog=mysql_query($query_blog);

    $num_blog=mysql_numrows($result_blog);

    mysql_close();

        $sql_index_menu="0";
        while ($sql_index_menu < $num) {

        $msg_subject=mysql_result($result,$sql_index_menu,"subject");
        $msg_id=mysql_result($result,$sql_index_menu,"id");
        $msg_from=mysql_result($result,$sql_index_menu,"from");
        $msg_to=mysql_result($result,$sql_index_menu,"recipient");
        $msg_text=mysql_result($result,$sql_index_menu,"text");
        $msg_time=mysql_result($result,$sql_index_menu,"time");
        $msg_read=mysql_result($result,$sql_index_menu,"readed");
        ?>
            <tr>
                <td><a href="?action=view&id=<?php echo $msg_id; ?>&lang=<?php echo $actLang; ?>"><?php if($msg_read == "0") {echo "<img src='/images/message.gif' width='32' height='32'>";} else {echo "<img src='/images/message.png' width='32' height='32'>";}?> <?php echo $msg_time; ?></a></td><td><a href="?action=view&id=<?php echo $msg_id; ?>&lang=<?php echo $actLang; ?>"><?php echo $msg_subject; ?></a></td><td><a href="?action=view&id=<?php echo $msg_id; ?>&lang=<?php echo $actLang; ?>"><?php echo $msg_from; ?></a></td>
            </tr>


    <?php
$sql_index_menu++;
}

everything work BUT, when i add this to $query_blog

$query_blog="SELECT * FROM messages WHERE recipent='$username'";

so it won't work..

I tryed to change $username with my name but it still not working.

This code is working, so I copyed it and still nothing...

            include "sql_connect.php";
    $query="UPDATE messages
            SET readed='1'
            WHERE id='$id'";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();
    include "sql_connect.php";
    $query_blog="SELECT * FROM messages WHERE id='$id'";
    $result_blog=mysql_query($query_blog);

    $num_blog=mysql_numrows($result_blog);

    mysql_close();


    $msg_text=mysql_result($result_blog,$sql_index_blog,"text");
    $msg_from=mysql_result($result_blog,$sql_index_blog,"from");
    $msg_subject=mysql_result($result_blog,$sql_index_blog,"subject");
    $msg_time=mysql_result($result_blog,$sql_index_blog,"time");

Can you help me?

I disabled login required to page so now you can see the page (sorry for language :D) As you can see, no error The website

as mentioned there is a typo, you misstyped recipient, anyway - i recommend you to use mysql_error() function to debug you'r code, an example would be:

$result=mysql_query($query) or die("<b>error:</b>".mysql_error()."line:".__LINE__);

The easiest way to debug a code in PHP is using echo or print_r.

In this case, you can include echo on $query_blog after setting it and run the result in your mysql IDE (or mysql command line).

$query_blog="SELECT * FROM messages WHERE recipent='$username'";    
echo $query_blog;

Also, it's not a good msyql practice using quotes on where because your code will be vulnerable to injections.

Instead, use this:

$result = sprintf("SELECT * FROM messages WHERE recipent='%s'", mysql_real_escape_string($username));

$result = mysql_query($query);

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "
";
    $message .= 'Whole query: ' . $result
    die($message);
}

If you are querying a mysql database from php and you want to use php variables in your query you have to escape them, otherwise you are passing the string '$username', not the value that is stored in $username.

Does this work for you?

$query_blog="SELECT * FROM messages WHERE recipent='" . $username . "'";
var_dump($query_blog);