如何将 mysql_num_rows 等函数更改为 mysqli?
How to change mysql_num_rows etc functions to mysqli?
我正要将我的 PHP
文件启动到免费的网络托管站点时遇到错误。出现的错误如下:
下面是我的项目的代码。
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysql_query($sql) or die("Error: " . mysql_error()); //this is error on line 42
$row = mysql_num_rows($query);
我不确定错误是什么,因为我是在PHP上自学的。希望你们能指出我应该做出什么改变。提前致谢!
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysql_query($sql) or die("Error: " . mysql_error()); //this is error on line 42
$row = mysql_num_rows($query);
试试这个
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysqli_query($con, $sql) or die("Error: " . mysqli_error($con)); // $con is the connection to database like // $con = mysqli_connect("localhost","my_user","my_password","my_db");
$row = mysqli_num_rows($query);
首先,正如您在标题中所建议的那样,出于安全原因使用 mysqli,或者更好的是 PDO。
使用 mysqli:(更新)
$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE staff_id = :staff_id AND password = :password");
$res = $stmt->execute(["staff_id" => $staff_id, "password" => $password);
$row = mysqli_num_rows($res);
使用 PDO:
$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE staff_id = :staff_id AND password = :password");
$res = $stmt->execute(["staff_id" => $staff_id, "password" => $password);
$row = $res->fetchColumn();
$conn
是您的数据库 link。 PDO 版本假定您不需要行,而只需要计数。如果有人告诉您,请不要在 SELECT
查询中使用 rowCount
,那是不可靠的。
我正要将我的 PHP
文件启动到免费的网络托管站点时遇到错误。出现的错误如下:
下面是我的项目的代码。
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysql_query($sql) or die("Error: " . mysql_error()); //this is error on line 42
$row = mysql_num_rows($query);
我不确定错误是什么,因为我是在PHP上自学的。希望你们能指出我应该做出什么改变。提前致谢!
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysql_query($sql) or die("Error: " . mysql_error()); //this is error on line 42
$row = mysql_num_rows($query);
试试这个
$sql= "SELECT * FROM user WHERE staff_id='$staff_id' AND password='$password'";
$query = mysqli_query($con, $sql) or die("Error: " . mysqli_error($con)); // $con is the connection to database like // $con = mysqli_connect("localhost","my_user","my_password","my_db");
$row = mysqli_num_rows($query);
首先,正如您在标题中所建议的那样,出于安全原因使用 mysqli,或者更好的是 PDO。
使用 mysqli:(更新)
$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE staff_id = :staff_id AND password = :password");
$res = $stmt->execute(["staff_id" => $staff_id, "password" => $password);
$row = mysqli_num_rows($res);
使用 PDO:
$stmt = $conn->prepare("SELECT COUNT(*) FROM user WHERE staff_id = :staff_id AND password = :password");
$res = $stmt->execute(["staff_id" => $staff_id, "password" => $password);
$row = $res->fetchColumn();
$conn
是您的数据库 link。 PDO 版本假定您不需要行,而只需要计数。如果有人告诉您,请不要在 SELECT
查询中使用 rowCount
,那是不可靠的。