如果数据库中存在单词,我希望在用户点击提交按钮后检查用户的输入。为什么mysqli_num_rows总是返回0?
因此,我有这个代码。然而,它总是说数据不存在,它发现条件为false。
我的代码在下面有什么问题?
$input_word = trim($_POST["word"]);
$data_check = "SELECT * FROM word_collections WHERE single_word = '$input_word'";
$a = mysqli_query($data_check);
if (mysqli_num_rows($a) == 1)
{
echo "Data exist.";
}
else
{
echo "Data DOES NOT exist";
}
您确定它不会返回超过1吗?更重要的是,你可以[SQL注入](http://php.net/manual/en/security.database.sql-injection.php)开放,并且应该使用[Prepared Statements](http:// php。 net/manual/en/mysqli.quickstart.prepared-statements.php)而不是串联你的查询。特别是因为你没有逃避用户输入! –
我读过一篇文章,mysqli_num_rows将只返回1或0 @MagnusEriksson –
否..它返回查询中返回的行数。 “num_rows”=“行数”。如果您有关于php函数的问题,您应该始终从soure开始:[php手册](http://php.net/manual/en/mysqli-result.num-rows.php) –