我只是PHP的新手,我需要创建一个php登录表单,但语法错误说Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\login.php on line 18
也许你可以帮我这个tnx!
<?php
$con=mysqli_connect("localhost","root","","reservation");
if (mysqli_connect_errno())
{
echo "Failed to connect to Database: " . mysqli_connect_error();
}
else
{
}
if ($_POST["SUBMIT"]="SUBMIT")
{
$username = $_POST['username'];
$password =$_POST['password'];
$sql ="SELECT * FROM usersaccount where='$username' and password ='$password'";
$result=mysqli_query($con,$sql);
$count=mysqli_num_rows($result);
if($count==1)
{
echo "success";
}
//mysqli_close($con);
}
?>
答案 0 :(得分:3)
您的 SELECT
声明错误...应该是
SELECT * FROM `usersaccount` WHERE `username` ='$username' and `password` ='$password'";
---------- // Here.. Added the column name
您未提供username
字段的列名称。那是你的问题。由于您将结果与条件匹配,因此您还需要 WHERE
条款!
免责声明:不推荐使用mysql_ *函数,因为它们已被弃用。改为使用MySQLi或PDO。
答案 1 :(得分:1)
这意味着您的查询(即$sql
)已损坏。您应该使用WHERE
子句时将表与字符串进行比较。此外,password
是保留字;逃避它,像这样:
$sql ="SELECT * FROM usersaccount WHERE `username` ='$username' and `password` ='$password'";
此外,您对SQL注入开放;你需要准备好的陈述。
答案 2 :(得分:1)
根据documentation,你必须这样做:
$con=mysqli_connect("localhost","root","","reservation");
//...
$query = "SELECT * FROM usersaccount ='$username' and password ='$password'";
if ($stmt = mysqli_prepare($con, $query)) {
mysqli_stmt_execute($stmt);/* execute query */
mysqli_stmt_store_result($stmt);/* store result */
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
mysqli_stmt_close($stmt);/* close statement */
}
答案 3 :(得分:0)
由于错误消息报告其第一个参数是布尔值,因此您应该仔细查看第一个参数。
$result=mysqli_query($con,$sql);
阅读the documentation:如果有效,mysqli_query()
如果不起作用,则返回mysqli_result()
或FALSE(布尔值)。所以,你的查询不起作用,它返回一个布尔值。
您应该检查执行查询的结果,如果失败,您应该显示生成的错误消息:
if ( $result = mysqli_query($con, $sql) === FALSE ) {
printf("Invalid query: %s\nWhole query: %s\n", mysqli_error, $sql);
exit();
}