我有一个名为Userdaten的colums id,Benutzername和Passwort。现在我想测试这段代码。它不起作用。我得到致命错误:在第11行的boolean上调用成员函数bindParam()。为什么? 我使用Server-Typ:MariaDB。
<?php
include 'conn.php';
$result="";
$username = "Max";
$password = "123";
$sql = "SELECT * FROM Userdaten WHERE Benutzername = :username AND Passwort = :password";
$stmt = $Userdaten->prepare($sql);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
$stmt->bindParam(':password', $password, PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount())
{
$result = "true";
}
elseif(!$stmt->rowCount())
{
$result = "false";
}
?>
<?php
$db_name = "testdata";
$mysql_username = "******";
$mysql_password = "******";
$server_name = "localhost";
$Userdaten = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
mysqli_query($Userdaten, "SET NAMES 'utf8'");
?>
答案 0 :(得分:-2)
尝试:
$sql = "SELECT * FROM Userdaten WHERE Benutzername = :username AND Passwort = :password";
$stmt = $Userdaten->prepare($sql);
$stmt->bindparam(":username",$username);
$stmt->bindparam(":password",$password);
$stmt->execute();
$result = ($stmt->rowCount() > 0) ? true : false;
此外 - 在您的数据库连接文件中,更改此:
$Userdaten = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
mysqli_query($Userdaten, "SET NAMES 'utf8'");
到此:
$Userdaten = new PDO("mysql:host=" . $server_name . ";dbname=" . $db_name . ";charset=utf8", $mysql_username, $mysql_password);
当您开始使用PDO时,请在db文件中使用相同的API。有关API的更多信息,您可以选择连接到MySQL的PHP项目是here。总而言之,该链接告诉我们以下内容:
不建议将旧的mysql扩展名用于new 开发,因为它在PHP 5.5.0中已被弃用,并在PHP中被删除 7。
请注意,上面的链接还包含各个API的详细功能比较矩阵。