[英]Correct Syntax, still gives error
我很难让它工作。 当我直接将其插入时,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>
首先,您应该使用绑定变量,而不是像这样将参数嵌入SQL中(为什么不请参见http://bobby-tables.com/ )。
您在问题中输入的错误消息与查询不匹配,除非$ userID包含“>”,这意味着您的消息或$ userID是整数的断言都是错误的。
我发现了错误。
echo "Created by ".findUserNameFromID($row['creatorUserID']."<br />");
括号echo "Created by ".findUserNameFromID($row['creatorUserID']."<br />");
在错误的位置,导致"<br />"
包含在$userID
。
自我注意事项:检查括号在哪里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.