Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果查询未找到任何匹配项_Php_Sqlite - Fatal编程技术网

Php 如果查询未找到任何匹配项

Php 如果查询未找到任何匹配项,php,sqlite,Php,Sqlite,我有一个网页,在客户端的计算机中创建一个名为token的cookie,当客户端登录时,该cookie中包含一个随机值。该cookie是从一个单独的登录页面创建的,不包括在这里,因为cookie的创建已经没有问题了 然后,我将来自客户端计算机的cookie令牌的值与SQLite3数据库中的令牌值进行匹配。如果两者都匹配,我将从与令牌相同的行中选择客户端的电子邮件 当前,如果cookie令牌的值和数据库令牌与预期匹配,则此方法有效。但是如果两个代币都不匹配,我就很难找出方法,所以就没有电子邮件显示了

我有一个网页,在客户端的计算机中创建一个名为token的cookie,当客户端登录时,该cookie中包含一个随机值。该cookie是从一个单独的登录页面创建的,不包括在这里,因为cookie的创建已经没有问题了

然后,我将来自客户端计算机的cookie令牌的值与SQLite3数据库中的令牌值进行匹配。如果两者都匹配,我将从与令牌相同的行中选择客户端的电子邮件

当前,如果cookie令牌的值和数据库令牌与预期匹配,则此方法有效。但是如果两个代币都不匹配,我就很难找出方法,所以就没有电子邮件显示了。确切地说,我有这个问题

if(!$email)
代码的区域,因为我不知道如何检查是否没有要返回的电子邮件

这是我的全部代码,请注意,像$url和$db这样的变量已经从我包含的外部*.php文件中传递

<?php include 'root.php';?>
<?php include ''.$root.'/database.php';?>

<?php

$token=$_COOKIE["token"];   
$auth = $db->query("select email, token from users where token='$token'");

if(!isset($token)){
    header('location: '.$url.'/login');
}
else {
    while ($row = $auth->fetchArray()){
        $email=$row['email'];
        if(!$email){
            echo "Didn't find any emails matching with the current cookie.";
        }
    }
}
?>
解决方案可以是:

$token = $_COOKIE["token"];
// Why execute a query if token is not set?
if(!isset($token)){
    header('location: '.$url.'/login');
    exit();
}

$auth = $db->query("select email, token from users where token='$token'");
// `fetchArray` fetches you row of FALSE if there are no rows.
$row = $auth->fetchArray();

if ($row) {
    $email=$row['email'];
} else {
    echo "Didn't find any emails matching with the current cookie.";
}
如果fetchArray方法返回一个结果,$row将始终是一个truthy值。您可以改为检查$row值是否为truthy,因为没有返回结果的查询将使fetchArray返回false。还可以检查是否设置了先前定义的变量。您应该检查是否设置了cookie,并在标头调用后退出。你也不需要像那样进出PHP,除非你做了一些在PHP中无法解析的事情-

<?php 
include 'root.php';
include $root.'/database.php';

if (!isset($_COOKIE["token"])) {
    header('location: '.$url.'/login');
    exit;
}

$token = $_COOKIE["token"];   
$auth = $db->query("SELECT email, token as cnt FROM users WHERE token='$token'");
$row = $auth->fetchArray();

if ($row) {
    // Email exists! Use $row['email'] and $row['token']
} else {
    // No rows matching the condition
}