![](/img/trans.png)
[英]Checking whether e-mail address exists; or at least whether it doesn't exist
[英]Checking if e-mail exists but returning empty query?
我正在尝试编写一些代码来检查表中是否存在电子邮件,但始终收到“查询为空”错误。 我似乎无法找到查询为什么返回空的原因?
我是PHP的新手,并且正在网上进行在线教程,因此我遵循的代码似乎相同。 我的代码:
recovery1.php
<?php require_once('Connections/localhost.php'); ?>
<?php
session_start();
if(isset($_SESSION['MM_Username'])) {
header("Location: My_Account.php");
die;
}
?>
<?php include('functions2.php'); ?>
<?php
$mode_allowed = array('username', 'password');
if (isset($_GET['mode']) === true && in_array($_GET['mode'], $mode_allowed) == true) {
if (isset($_POST['email']) === true && empty($_POST['email']) === false) {
if (email_exists(($_POST['email'])) === true) {
echo "ok";
} else {
echo '<div id="error"> We could not find that email address, please try again. </div>';
}
}
?>
<?php
} else {
header('Location: subscribe.php');
exit();
}
?>
functions2.php
<?php
function sanitize($data) {
return mysql_real_escape_string($data);
}
function recover ($mode, $email) {
$email = sanitize($email);
$mode = sanaitize($mode);
$user_data = user_data(UserID_from_email($email), `Username`);
if ($mode == 'username') {
email($email, 'Your Username', "Hi/n As requested your username is " . $user_data['Username'] . "/n/n Infinity Crates");
} else if ($mode == 'password') {
//recover password
}
}
function email_exists($email) {
$email = sanitize($email);
$query = mysql_query("SELECT COUNT (`UserID`) FROM `users` WHERE `Email` = '$email'");
$result = mysql_query($query) or die(mysql_error());
return (mysql_result($result, 0) == 1) ? true : false;
}
function UserID_from_email($email) {
$email = sanitize($email);
return mysql_result(mysql_query("SELECT `UserID` from `Users` WHERE `Email` = '$email'"), 0, `UserID`);
}
function email($to, $subject, $body) {
mail($to, $subject, $body, 'From: infinitycrate@gmail.com');
}
?>
您正在使用mysql_query 2次。
做了:
function email_exists($email) {
$email = sanitize($email);
$query = "SELECT COUNT (`UserID`) FROM `users` WHERE `Email` = '$email'";
$result = mysql_query($query) or die(mysql_error());
return (mysql_result($result, 0) == 1) ? true : false;
}
您甚至可以像这样修改函数:
function email_exists($email) {
$email = sanitize($email);
$result = mysql_query("SELECT * FROM `users` WHERE `Email` = '$email'") or die(mysql_error());
return (mysql_num_rows($result) > 0) ? true : false;
}
注意:PHP 5.5不推荐使用默认的Mysql API。 请改用PDO和Mysqli。 有关更多信息,请参见http://php.net/manual/zh/mysqlinfo.api.choosing.php
您的函数要么需要全局化mysql_connect资源变量,要么将资源传递给函数。 检查您的第一个require_once文件,并查看mysql_connect资源是否存储在变量中。 如果不是,请对其进行修改以将资源保存到$dbh
类的变量中,并通过插入global $dbh;
修改“ email_exists”,“ sanitize”和“ UserID_from_email”函数global $dbh;
作为这些功能的第一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.