没有显示mysql选择all的结果

时间:2011-10-02 10:58:15

标签: php mysql sql

大家好我试图向所有用户显示口袋妖怪是表belongssto =这里的用户名是我的代码 我有一个连接在这上面很好

     // Get all the data from the "example" table
$result = "SELECT * FROM user_pokemon WHERE
    belongsto='".$_SESSION['username']."'
AND (slot='0')'"; 
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table

    echo $row['pokemon'];

    echo $row['id'];

} 

我打印红色用户名,用户名会话中有用户名。 我想我会在查询结束时错过一个'或者我添加的东西或mysql但是然后页面就会死掉而没有错误

2 个答案:

答案 0 :(得分:4)

您没有运行查询并且在其中有错误。而且你没有逃避进入查询的字符串。 正确版本的代码将是

// escape a string going to query.
$username = mysql_real_escape_string($_SESSION['username']);
// create a query
$sql = "SELECT * FROM user_pokemon WHERE belongsto='$username' AND slot=0"; 
// run a query and output possible error for debugging purposes.
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
// keep getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
  echo $row['pokemon'];
  echo $row['id'];
} 

答案 1 :(得分:1)

在我看来,最终的查询将是:

SELECT * FROM user_pokemon WHERE belongsto='NAME' AND (slot='0')'

其中NAME是您传入的名称。如果是这种情况,最后会有一个额外的单引号。我认为你得到一个SQL错误?