我正在使用准备好的语句转换为PDO和Im,因此我想将结果绑定在一起,$stmt->bind_result($email_count);
因此我可以将其放入if语句中,以查看电子邮件是否存在,但是我遇到了Fatal error: Call to undefined method PDOStatement::bind_result() in /Applications/XAMPP/xamppfiles/htdocs/imanage/insert.php on line 51
与上一个相关的错误例。
我猜bind_result不是PDO定义的方法,所以有没有可以使用的等效方法?
我的代码在下面,以防万一:
insert.php
<?php
include("connect/class.Database.php");
class Users extends Database {
public function insert() {
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM users WHERE email=:email");
$stmt->bindParam(":email", $_POST['email']);
$stmt->bind_result($email_count);
$stmt->execute();
$stmt->fetch(PDO::FETCH_ASSOC);
if ($email_count > 0) {
echo "email exisits! click here to try <a href='register'>again</a>";
} else {
//escape the POST data for added protection
$username = isset($_POST['username']) ? $_POST['username'] : null;
$cryptedPassword = crypt($_POST['password']);
$password = $cryptedPassword;
$name = isset($_POST['name']) ? $_POST['name'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$data = array($username, $password, $name, $email);
$stmta = $this->pdo->prepare("INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, ?)");
$stmta->execute($data);
printf("%d Row inserted.\n", $stmta->row_count);
/* close statement and connection */
$stmta->close();
} // end email_count and insert to table
} // end function
}
?>
connect / class.Database.php
<?php
// Database connection PDO
class Database {
public function __construct() {
// Connection information
$host = 'localhost';
$dbname = 'imanage';
$user = 'root';
$pass = '';
// Attempt DB connection
try
{
$this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Successfully connected to the database!';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function __destruct()
{
// Disconnect from DB
$this->pdo = null;
echo 'Successfully disconnected from the database!';
}
}
?>
然而,您也无需数数。请避免不必要的操作-它们只会无故膨胀和混淆您的代码。
首先考虑一下,查询需要什么?您真的需要数数吗?否。您实际需要的只是一个标志-用户是否存在。因此,进行查询以返回此类标志。
$stmt = $this->pdo->prepare("SELECT 1 FROM users WHERE email=?");
$stmt->execute(array($_POST['email']));
$exists = $stmt->fetchColumn();
其他所有代码也一样
//escape the POST data for added protection
您实际上并没有在此代码块中“转义”任何数据,也不添加任何保护。但是,我认为插入NULL作为电子邮件绝对没有意义。你确定你真的想要吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句