PDO :: FETCH_ASSOC返回false
问题描述:
这是一个脚本,用于搜索数据库中的用户。我将这个脚本分成4个文件:login.class.php,userLookup.php,userLookupDisplay.tpl.php和userLookup.tpl.php。PDO :: FETCH_ASSOC返回false
的代码在这些文件如下:
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);
}
不应该有一些'和'S之间那个'$ where'变量中的语句? – adeneo 2015-04-04 04:35:32