无法计算成功执行的 mysqli 查询
Cannot count successfully executed mysqli query
我想看看我的查询是否成功(好吧,虽然我成功地执行了插入查询)但是在第 25 行出现了这个错误:"mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given "我已经检查了每一行,我什至使用mysqli_affected_rows 失败了。感谢任何帮助,这是我的代码:
<?php
include "connection.php";
$fn = $_POST['first_name'];
$ln = $_POST['last_name'];
$uname = $_POST['username'];
$pwd = $_POST['password'];
$stat = $_POST['status'];
if($fn=="" || $ln=="" || $uname=="" || $pwd=="" || $stat==""){
echo "ALL FIELD MUST BE FILLED!";
}
else {
$sql = "SELECT * from user WHERE username='$uname'";
$result = mysqli_query($conn, $sql);
$number = mysqli_num_rows($result);
if ($number != 0) {
echo "Username $uname is not available";
}
else {
$options = ['cost' => 12];
$pwd = password_hash($pwd, PASSWORD_BCRYPT, $options);
$query = "INSERT INTO user
(username,password,first_name,last_name,status) VALUES
('$uname','$pwd','$fn','$ln','$stat')";
$res = mysqli_query($conn, $query);
$nmb = mysqli_num_rows($res);
echo $nmb;
}
}
?>
改变这个:
$res = mysqli_query($conn, $query);
$nmb = mysqli_num_rows($res);
echo $nmb;
为此:
$res = mysqli_query($conn, $query);
if ($res) {
echo "Insert ok";
}
else
{
echo "Problem to insert";
}
$nmb = mysqli_num_rows($res);
无法工作,因为 $res
是 INSERT 查询的结果。 The documentation for mysqli_query
说:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
因此,$res
将始终是 false
或 true
,您将无法使用 mysqli_num_rows()
或 mysqli_affected_rows()
它。问题是,不管怎样,在这里计算行数并没有什么意义。您正在尝试插入一条记录,因此它要么是 1 要么是 0,因此 true 或 false 结果应该足以让您检测查询是否成功。
我想看看我的查询是否成功(好吧,虽然我成功地执行了插入查询)但是在第 25 行出现了这个错误:"mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given "我已经检查了每一行,我什至使用mysqli_affected_rows 失败了。感谢任何帮助,这是我的代码:
<?php
include "connection.php";
$fn = $_POST['first_name'];
$ln = $_POST['last_name'];
$uname = $_POST['username'];
$pwd = $_POST['password'];
$stat = $_POST['status'];
if($fn=="" || $ln=="" || $uname=="" || $pwd=="" || $stat==""){
echo "ALL FIELD MUST BE FILLED!";
}
else {
$sql = "SELECT * from user WHERE username='$uname'";
$result = mysqli_query($conn, $sql);
$number = mysqli_num_rows($result);
if ($number != 0) {
echo "Username $uname is not available";
}
else {
$options = ['cost' => 12];
$pwd = password_hash($pwd, PASSWORD_BCRYPT, $options);
$query = "INSERT INTO user
(username,password,first_name,last_name,status) VALUES
('$uname','$pwd','$fn','$ln','$stat')";
$res = mysqli_query($conn, $query);
$nmb = mysqli_num_rows($res);
echo $nmb;
}
}
?>
改变这个:
$res = mysqli_query($conn, $query);
$nmb = mysqli_num_rows($res);
echo $nmb;
为此:
$res = mysqli_query($conn, $query);
if ($res) {
echo "Insert ok";
}
else
{
echo "Problem to insert";
}
$nmb = mysqli_num_rows($res);
无法工作,因为 $res
是 INSERT 查询的结果。 The documentation for mysqli_query
说:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
因此,$res
将始终是 false
或 true
,您将无法使用 mysqli_num_rows()
或 mysqli_affected_rows()
它。问题是,不管怎样,在这里计算行数并没有什么意义。您正在尝试插入一条记录,因此它要么是 1 要么是 0,因此 true 或 false 结果应该足以让您检测查询是否成功。