我将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上自学的。希望你们能指出我应该做出的改变。预先感谢!
答案 0 :(得分:0)
$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);
答案 1 :(得分:0)
首先,正如您在标题中所建议的那样,出于安全原因,甚至更好地使用PDO,请使用mysqli。
使用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
是您的数据库链接。 PDO版本假定您不需要行,而只需要计数。如果有人告诉您,请勿在{{1}}查询中使用rowCount
,这是不可靠的。