我无法让这个工作。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>