查询存在的查询 - PHP

时间:2014-06-24 14:41:21

标签: php mysql database

我正在尝试通过运行查询检查来测试我的数据库中是否存在电子邮件地址。

我可以很好地连接到数据库。

然而无论如何,即使电子邮件存在,它也会返回“不存在”。

<?php

//----------------------------------------------------------------------------------//
//Setup
require_once('SB_Constants.php');
//----------------------------------------------------------------------------------//


//Connect to the database
//----------------------------------------------------------------------------------//
$connection = mysqli_connect(DATABASE_HOST, SAVE_USERNAME, SAVE_PASSWORD, DATABASE_NAME);

// check the connection was successful
if (mysqli_connect_errno($connection)) {
    header('HTTP/1.0 500 Internal Server Error', true, 500);
    die(FailedToAccessDatabase . ". Failed to connect to Database");
} else {
    echo "Connection Success!";
}


//Query Check
$assessorEmail = mysqli_query($connection, "SELECT email_address FROM assessorID WHERE email_address = 'ryan@ablah.com'");
if (mysqli_num_rows($query_identifier) == 0) {
    die(UnregisteredAssessor . ". Doesn't Exist");
} else {

    // Exists 
    echo "Exists getting ace id.";

    //Get the assessor ID
    $result = mysqli_query($connection, "SELECT ace_id FROM assessorID WHERE email_address = 'ryan@blah.com'");
    echo $result;
}
/* close connection */
mysqli_close($connection);

?>

问题的任何想法? :)

2 个答案:

答案 0 :(得分:3)

各种错误。修正:

$assessorEmail = mysqli_query($connection, "SELECT ace_id,email_address FROM assessorID WHERE email_address = 'ryan@ablah.com'");
if (mysqli_num_rows($assessorEmail) == 0) {
    die(UnregisteredAssessor . ". Doesn't Exist");
} else {

    // Exists 
    echo "Exists getting ace id.";

    //Get the assessor ID
    $result = mysqli_fetch_assoc($assessorEmail);
    echo $result['ace_id'];
}

答案 1 :(得分:2)

您的问题是mysqli_num_rows($query_identifier)正在访问未定义的变量,而不是$assessorEmail

此外,如果您只想要ace_id

,则只需要一个查询
$assessorEmail = mysqli_query($connection, "SELECT ace_id FROM assessorID WHERE email_address = 'ryan@ablah.com'");

如果mysqli_num_rows($assessorEmail)返回一行,则表明该电子邮件已存在且您已经拥有ace_id

while(mysqli_fetch_assoc($assessorEmail) = $row) {
  echo $result['ace_id'];
}