如何检查MySQL查询是否返回结果

时间:2017-09-27 15:37:00

标签: php mysql if-statement

我在XAMPP服务器上运行此代码,该服务器连接到db.php中的数据库。

<?php
require 'db.php';
session_start();

$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['hash']) && !empty($_GET['hash']))
{
    $email = $mysqli->escape_string($_GET['email']); 
    $hash = $mysqli->escape_string($_GET['hash']); 

    $mysqli->query("SELECT * FROM users WHERE email='$email' AND hash='$hash' AND active='1'");
    if (@@ROWCOUNT <> 1) {
        $_SESSION['message'] = "Account has already been activated";

        echo '<script type="text/javascript">
           window.location = "/html/rtl/profile.php"
      </script>';
    }
}

我试图在PHP中使用if语句来查看我的MySQL搜索是否返回结果,但它无法正常工作。我该怎么做?

1 个答案:

答案 0 :(得分:-1)

试试这个:

$query = $mysqli->query("SELECT * FROM users WHERE email='$email' AND hash='$hash' AND active='1'");
if($query->num_rows<>1) {
   //Rest of the code
}