php - PDO::FETCH_ASSOC 返回 false

标签 php mysql sql oop pdo

这是一个应该在数据库中搜索用户的脚本。我将此脚本分为 4 个文件:login.class.php、userLookup.php、userLookupDisplay.tpl.php 和 userLookup.tpl.php。

这些文件中的代码如下:

login.class.php:(这不是整个类,为了便于阅读,我删除了大部分类)

class login
{
    public function userLookup($email = null, $first_name = null, $last_name = null)
    {
        $where = "";
        $execute = "";
        if(!is_null($email) && !empty($email))
        {
            $where .= "email = :email";
            $execute = array(':email' => $email);
        }

        if(!is_null($first_name) && !empty($first_name))
        {
            $where .= "first_name = :firstName";
            $execute = array(':firstName' => $first_name);
        }

        if(!is_null($last_name) && !empty($last_name))
        {
            $where .= "last_name = :lastName";
            $execute = array(':lastName' => $last_name);
        }

        if (!empty($where))
        {
        $query =$this->_db->prepare("SELECT id, email, first_name, last_name FROM users WHERE " . $where);
        $query->execute($execute);
        return $query->fetch(PDO::FETCH_ASSOC);
        var_dump($query);
        var_dump($execute);
        }

        elseif(empty($where))
        {
            echo "Please enter at least one search criteria";
        }
    }   
}

userLookup.php:

session_start();

require_once('inc/config.php');
require_once($loginClassPath);

if (!empty($_SESSION['user_id']) && $_SESSION['role'] == 2)
{
        include('inc/dbConnect.php');
        if ($pdo)
        {
            $loginClass = new login($pdo);
            $userData = null;

            if (isset($_POST['submit']))
            {
                $firstName = $_POST['first_name'];
                $lastName = $_POST['last_name'];
                $email = $_POST['email'];
                $userData = $loginClass->userLookup($email, $firstName, $lastName);
                var_dump($userData);
            }

            if (!is_null($userData))
            {
                include($userLookupDisplayTpl);
            }
            else
            {
                include($userLookupTpl);
            }
        }
}
else
{
    header('Location: accessDenied.php');
    die();
}

userLookup.tpl.php:

<html>
<head>

<title>View Users</title>
</head>
<body>

<div style="text-align:center;">
        <form method="POST" action="<?= $_SERVER['SCRIPT_NAME']; ?>">
            <? var_dump($userData); ?>
            First Name: <input type="text" name="first_name" value=""/><br>
            Last Name: <input type="text" name="last_name" value=""/><br>
            Email: <input type="text" name="email" value=""/><br>            
            <input type="submit" name="submit" value="Get User"/>
        </form>
</div>

</body>
</html>

userLookupDisplay.tpl.php:

<html>
<head>

<title>View Users</title>
</head>
<body>

<div style="text-align:center;">
    <? var_dump($userData); ?>
    <? echo $userData['id']; ?>
    <? echo $userData['first_name']; ?>
    <? echo $userData['last_name']; ?>
    <p>Return</p>
</div>

</body>
</html>

我遇到的问题是 $userData(在 userLookup.php 中)不断返回 boolean false 而没有任何错误。基于 var_dumps 看起来查询本身正在正确构造,所以我无法弄清楚为什么它返回 false?任何和所有的帮助表示赞赏。提前谢谢大家!另外,如果您需要更多信息,请告诉我。

最佳答案

在您的 login.class.php 中,您覆盖了数组 $execute,并且您需要一些条件,例如 AND OR 到您的 WHERE 子句,将您的代码更改为:

    $where = array();
    $execute = array();

    if(!is_null($email) && !empty($email))
    {
        $where[] = "email = :email";
        $execute[':email'] = $email;
    }

    if(!is_null($first_name) && !empty($first_name))
    {
        $where[] = "first_name = :firstName";
        $execute[':firstName'] = $first_name;
    }

    if(!is_null($last_name) && !empty($last_name))
    {
        $where[] = "last_name = :lastName";
        $execute[':lastName'] = $last_name;
    }

    if (count($where) > 0)
    {

    $where = implode(' AND ', $where);

    $query =$this->_db->prepare("SELECT id, email, first_name, last_name FROM users WHERE " . $where);
    $query->execute($execute);
    return $query->fetch(PDO::FETCH_ASSOC);
    var_dump($query);
    var_dump($execute);
    }

关于php - PDO::FETCH_ASSOC 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442707/

相关文章:

PHP DateTime::createFromFormat 和多语言

php - 取消设置变量组

php - 调用未定义的方法 Illuminate\Database\Query\Builder::has_many()

mysql - 检查是否所有记录在一个字段中具有相同的值?

mysql - 无法访问内部作用域中的变量?

javascript - 从服务器文件夹下载文件

mysql - 导入 10 GB SQL 文件

mysql - Airflow需要mysql吗?

sql - 大量行的表设计选项?

php - TestLink - 数据库访问错误 - debug_print_backtrace() 输出启动 (CWE-200)