gpt4 book ai didi

php - 试图理解 PHP 中的 PDO;不断收到空错误

转载 作者:行者123 更新时间:2023-11-28 23:42:38 26 4
gpt4 key购买 nike

我想弄清楚为什么我的 PDO 方法一直返回空值。从准备到执行,一切似乎都是正确的。

i.e. "Fatal error: Call to a member function prepare() on null in /blah/register.php on line 5"

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
if ($email === ''){
unset($email);
}else{
$query = $mysqli->prepare('SELECT email FROM `Members` WHERE email=:input_email');
$query->execute(array(':input_email' => $email));
foreach ($query as $row) {
echo $row['email'] . "<br />";
}
var_dump($query);
die();
}

此代码应回显所有值,然后终止脚本的其余部分。

数据库连接:

$db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8', 'dbuser', 'password', array(PDO::ATTR_EMULATE_PREPARES => false, 
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT));
if ($db->connect_errno) {
echo "<b>Sorry, this website is experiencing problems. </b>";
exit;
}

最佳答案

您在代码中混合了 mysqli 和 pdo,您需要更改 mysqli prepare with pdo prepare`

 $query = $db->prepare('SELECT email FROM `Members` WHERE email=:input_email');
^^^ //add pdo object here

您需要从执行语句中添加获取数据

 $query = $db->prepare('SELECT email FROM `Members` WHERE email=:input_email');
$query->execute(array(':input_email' => $email));
$result = $query->fetch(PDO::FETCH_ASSOC);
print_r($result);

阅读http://php.net/manual/en/pdostatement.fetch.php

关于php - 试图理解 PHP 中的 PDO;不断收到空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com