0

我无法让这个工作。mySQL 语句在我直接插入时起作用,并且 $dbc 是正确的,因为它在脚本中较早地提取了它。

我可以帮忙吗?

//gets the username from the users table by looking for given id
function findUserNameFromID($userID){
   global $dbc;
   $getUser_query = 'SELECT username FROM forum_users WHERE userID ='.$userID;
   echo $getUser_query;
   //Errors on this line:
   $results = mysqli_query($dbc, $getUser_query) or die("Error: ".mysqli_error($dbc));
   $row = mysqli_fetch_array($results);
   return $row['username'];
}

编辑:忘记错误:错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“>”附近使用正确的语法

编辑:这是整页代码:

    //gets the username from the users table by looking for given id
    function findUserNameFromID($userID){
        global $dbc;
        $getUser_query = 'SELECT username FROM forum_users WHERE userID ='.$userID;
        echo $getUser_query;
        $results = mysqli_query($dbc, $getUser_query) or die("Error: ".mysqli_error($dbc)." ".__LINE__);
        $row = mysqli_fetch_array($results);
        return $row['username'];
    }

    function findUserIDFromName($username){
        global $dbc;
        $getUser_query = "SELECT userID FROM forum_users WHERE username = $username";
        $results = mysqli_query($dbc, $getUser_query);
        $row = mysqli_fetch_array($results);
        return $row['userID'];
    }

    $getThreads_query = "SELECT * FROM forum_threads ORDER BY lastTime, id";

    $results = mysqli_query($dbc, $getThreads_query);

    // show the data
    while ($row = mysqli_fetch_array($results))
    {
       echo "<p>";
       echo $row['title']."<br />";
       echo "Created by ".findUserNameFromID($row['creatorUserID']."<br />");
       //echo "Created on ".$row['createDate'];
       echo "</p>";

    }

    mysqli_close($dbc);

    ?>
    </body>
</html>
4

2 回答 2

0

首先,您应该使用绑定变量,而不是像这样在 SQL 中嵌入参数(请参阅http://bobby-tables.com/了解为什么不这样做)。

您在问题中输入的错误消息与查询不匹配,除非 $userID 包含“>”,这意味着您的消息或 $userID 是整数的断言不正确。

于 2013-02-11T22:04:02.477 回答
0

我发现了错误。

中的右括号位于echo "Created by ".findUserNameFromID($row['creatorUserID']."<br />");错误的位置,导致"<br />"被包含在$userID.

自我注意:检查你的括号在哪里。

于 2013-02-15T04:08:35.437 回答