2

这是我的桌子

Field                   Type            Null    Key     Default  Extra
id_key                  int(11)         NO      PRI     NULL     auto_increment
tbl_users_username      varchar(255)    YES     UNI     NULL
tbl_users_password      varchar(32)     YES             NULL
tbl_users_identifier    varchar(32)     YES             NULL
tbl_users_token         varchar(32)     YES             NULL
tbl_users_access_type   int(1)          YES             NULL
tbl_users_timeout       int(10)         YES             NULL

这是我的代码

$query = "SELECT * FROM tbl_users where tbl_users_username = '$_POST[email_address]'" ;
$result = mysql_query($query);
if($result)
{
    echo "TRUE";        
}
else
{
    echo "FALSE";
}

连接到数据库没有问题。当我运行查询时出现问题。无论电子邮件是否在表中,它都会返回 $result。我在这里想念什么?如果电子邮件地址存在于表中,它应该回显 true,如果它不在表中,则返回 false。

4

6 回答 6

1

$_POST[email_address]我猜应该使用单引号或双引号:

$_POST["email_address"]或者$_POST['email_address']

$query = "SELECT * FROM tbl_users where tbl_users_username = '" . $_POST['email_address'] . "'" ;

但是,正如@FabioCosta 所说,您必须获取结果或计算返回的行数才能实现您的目标:

$query = "SELECT 1 FROM tbl_users where tbl_users_username = '" . $_POST['email_address'] . "'";
$result = mysql_query($query);
if(mysql_num_rows($result) === 1) {
    echo "TRUE";        
} else {
    echo "FALSE";
}
于 2012-06-12T19:00:35.740 回答
1

尽管如此,结果仍将返回(如果您进行了有效查询)使用mysql_num_rows 来检查邮件是否存在。如果可以,实际使用 PDO

于 2012-06-12T19:02:07.350 回答
1

如果您使用 PHP 的mysql_num_rows函数,您应该能够返回行数,并且您的逻辑将起作用。因此,请尝试以下操作:

$num = mysql_num_rows($result);
if ($num > 0) {
    echo "TRUE";
}else{
    echo "FALSE";
}
于 2012-06-12T19:02:28.810 回答
1

I suggest that you print out your query to ascertain that it is 100% the query you expect it to be.

In addition to that, You are checking:

$result = mysql_query($query);

if($result)

This checks if a resource was returned from your query. It your query is valid then as per the documentation, you should always have a resource.

Suggested code:

$result = mysql_query($query);

if($result)
{
    $num = mysql_num_rows($result);
    if ($num > 0)
    {
        //do logic processing here...
        echo "TRUE"; 
    }
}
else
{
   die('Invalid query: ' . mysql_error());
}

Code Explanation:

  1. Fetch the resource from the query
  2. If valid resource was returned, continue and check the number of records that were fetched from this query.
  3. If the resource was not returned, the else part echo out a failure message.

I would strongly suggest that you look into using PDO library.


Briefly the Advantages of PDO:

  1. Allows you to use prepared statement with ease (prepared statement are great for security!!!)

  2. PDO can connect to various different Databases including MySQL, but others as well.

  3. Its quite easy to use in my opinion and it quite easy to pick for new comers etc.


Stack overflow usage tips:

  1. Always use the search in the top right hand corner. Numerous people have come across problems that may could help you.
  2. Always have a look at the How to Ask Question FAQ
  3. Feedback & always ask further questions if required!
于 2012-06-12T19:53:08.930 回答
0

以下查询将根据发布的 email_address 值中的值选择 tbl_users_username 值。所以,使用这个查询:

$query = 
"SELECT * FROM tbl_users where tbl_users_username = '".$_POST['email_address']."';";
于 2012-06-12T19:01:38.740 回答
0
$query = "SELECT *
FROM tbl_users
WHERE tbl_users_username = '" . mysql_real_escape_string($_POST['email_address']) . "'";

$result = mysql_query($query) or die(mysql_error());

if($result)
{
    echo "TRUE";        
}
else
{
    echo "FALSE";
}

这应该可以解决您遇到的任何问题,或者至少给您一个方便的错误消息来解决。

于 2012-06-13T01:18:16.733 回答