从 MySQL 转换后 PDO 脚本不工作
Posted
技术标签:
【中文标题】从 MySQL 转换后 PDO 脚本不工作【英文标题】:PDO Script Not Working After Converting from MySQL 【发布时间】:2015-01-22 21:55:24 【问题描述】:提前感谢您的帮助。我在第一部分有一个带有 AJAX 电子邮件收集表单的登录页面,我改编自 Tutsplus tutorial。
但是,表单的 php 部分是用 mysql 编写的,它与我的 Heroku 服务器不兼容,因为它已被弃用。在 *** 上听取其他人的建议,我尝试将代码中的 MySQL 语句转换为 PDO。但是很难让表单正常工作。
这是原始的工作 MySQL 代码
<?php
if($_GET['action'] == 'signup')
// if(isset($_GET['action'])&& $_GET['action'] == 'signup')
mysql_connect('hostname','username','password');
mysql_select_db('databasename');
$email = mysql_real_escape_string($_POST['emailfield']);
//validate email address - check if input was empty
if(empty($email))
$status = "error";
$message = "You did not enter an email address!";
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid email address!";
else
$existingSignup = mysql_query("SELECT * FROM table WHERE signup_email_address='$email'");
if(mysql_num_rows($existingSignup) < 1)
date_default_timezone_set('Asia/Singapore');
$date = date('Y-m-d');
$time = date('H:i:s');
$insertSignup = mysql_query("INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
if($insertSignup) //if insert is successful
$status = "success";
$message = "You have been signed up!";
else //if insert fails
$status = "error";
$message = "Oops, Theres been a technical error!";
else //if already signed up
$status = "error";
$message = "This email address has already been registered!";
//return json response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
?>
这是我将上述 MySQL 代码转换为 PDO 的失败尝试
<?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup')
try
$db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8','user','pwd');
catch(exception $e)
echo "Error, connection failure";
$email = $_POST['emailfield'];
//validate email address - check if input was empty
if(empty($email))
$status = "error";
$message = "You did not enter an email address!";
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid email address!";
else
$existingSignup = "SELECT COUNT(*) FROM table WHERE signup_email_address='$email'";
if ($res->fetchColumn() < 1)
date_default_timezone_set('Asia/Singapore');
$date = date('Y-m-d');
$time = date('H:i:s');
$insertSignup = "INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES (:signupemail, :signupdate, :signuptime)";
$query = $db->prepare($insertSignup);
$query->bindParam(':signupemail', $email);
$query->bindParam(':signupdate', $date);
$query->bindParam(':signuptime', $time);
$results = $query->execute();
if($insertSignup) //if insert is successful
$status = "success";
$message = "You have been signed up!";
else //if insert fails
$status = "error";
$message = "Oops, Theres been a technical error!";
else //if already signed up
$status = "error";
$message = "This email address has already been registered!";
//return json response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
?>
您能给我建议以更正我的 PDO 代码吗?谢谢!
更新:
这是我更新的 PDO 代码:
<?php
if(isset($_GET['action'])&& $_GET['action'] == 'signup')
try
$db = new PDO('mysql:host=hostname;dbname=dbname;charset=utf8','user','pwd');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
error_reporting(-1);
catch(PDOexception $e)
echo "Error, connection failure";
$email = $_POST['emailfield'];
//validate email address - check if input was empty
if(empty($email))
$status = "error";
$message = "You did not enter an email address!";
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid email address!";
else
$existingSignup = "SELECT * FROM table WHERE signup_email_address='$email'";
$stmt = $db->query($existingSignup);
$row_count = $stmt->rowCount();
if ($row_count < 1)
date_default_timezone_set('Asia/Singapore');
$date = date('Y-m-d');
$time = date('H:i:s');
$insertSignup = "INSERT INTO table (signup_email_address, signup_date, signup_time) VALUES (:signupemail, :signupdate, :signuptime)";
$query = $db->prepare($insertSignup);
$query->bindValue(':signupemail', $email);
$query->bindValue(':signupdate', $date);
$query->bindValue(':signuptime', $time);
$results = $query->execute();
if($insertSignup) //if insert is successful
$status = "success";
$message = "You have been signed up!";
else //if insert fails
$status = "error";
$message = "Oops, Theres been a technical error!";
else //if already signed up
$status = "error";
$message = "This email address has already been registered!";
//return json response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
?>
但这是我得到的错误:致命错误:在第 27 行对 index.php 中的非对象调用成员函数 query()。
第 27 行:
$stmt = $db->query($existingSignup);
我该如何解决这个问题?
【问题讨论】:
我不确定,但可能会抛出非对象错误,因为您的 DVD 连接不好。所以我建议验证 PDO 类的参数(即 dbname、用户名、密码和主机名) 看看这个问题:***.com/questions/15297339/…。我在之前的评论中有一个错字我指的是变量$db
而不是 DVD .. 哈哈
谢谢哥们,在你的帮助下我搞定了!
【参考方案1】:
您的$existingSignup
查询未执行。你必须使用类似的东西:
$stmt = $db->query($existingSignup);
$row_count = $stmt->rowCount();
echo $row_count.' rows selected
然后继续:if ($row_count < 1) ...
了解更多信息。查阅此链接:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
【讨论】:
以上是关于从 MySQL 转换后 PDO 脚本不工作的主要内容,如果未能解决你的问题,请参考以下文章