[英]PDO::FETCH_ASSOC returns false
This is a script that is supposed to search for users in the DB. 这是一个应该在数据库中搜索用户的脚本。 I have this script split between 4 files: login.class.php, userLookup.php, userLookupDisplay.tpl.php, and userLookup.tpl.php.
我将这个脚本分为4个文件:login.class.php,userLookup.php,userLookupDisplay.tpl.php和userLookup.tpl.php。
The code in these files is as follows: 这些文件中的代码如下:
login.class.php: (this is not the entire class, I cut most of the class out for readability) 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: 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: 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: 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>
The problem I am having is that $userData
(in userLookup.php) keeps returning boolean false
without any errors. 我遇到的问题是
$userData
(在userLookup.php中)不断返回boolean false
而没有任何错误。 Based on the var_dumps it looks like the query itself is being constructed properly so I can't figure out why it's returning false? 基于var_dumps,看起来查询本身正在正确构建,因此我不知道为什么它返回false? Any and all help is appreciated.
任何和所有帮助表示赞赏。 Thanks in advance guys!
在此先感谢大家! Also, let me know if you need more info.
另外,如果您需要更多信息,请告诉我。
In your login.class.php
you override your array $execute
, and you need some condition like a AND
or OR
to your WHERE
clause, change your code to: 在您的
login.class.php
您将覆盖数组$execute
,并且需要对WHERE
子句使用AND
或OR
类的条件,将代码更改为:
$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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.