最近我正在使用PHP PDO,我刚决定重新开发整个数据库系统。所以问题就在这里,我创建了一个名为QueryRepository的类,它所拥有的是来自数据库的基本查询,它与我的数据库建立了连接。当我调用此查询并打印输出时,我收到这是输出:
QueryRepository Object([_database:QueryRepository:private] =>数据库对象([_ error:数据库:私有] =>)){"成功":1,"消息":"登录成功!"}
我不明白,因为选择查询我不应该收到这个表还是假的?忽略json。干杯们,这对我来说真的很奇怪。
$QueryRepository = new QueryRepository();
$QueryRepository->fetchClient($_POST['email'], $_POST['password']);
print_r($QueryRepository);
这就是出错的地方^
以下是我的文件。
的index.php
<?php
require_once('../core/init.php');
$login_ok = false;
$validated_info = false;
if (!empty($_POST)) {
$QueryRepository = new QueryRepository();
$QueryRepository->fetchClient($_POST['email'], $_POST['password']);
print_r($QueryRepository);
if($QueryRepository != false || $QueryRepository != null) {
$login_ok = true;
$response['success'] = 1;
$response['message'] = 'Login Successfull!';
die(json_encode($response));
} else {
$login_ok = false;
$response['success'] = 0;
$response['message'] = 'Incorrect Credentials!';
die(json_encode($response));
}
} else {
echo ' <h1>Login</h1>
<form action="index.php" method="post">
email:<br />
<input type="text" name="email" placeholder="email" />
<br /><br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>';
}
QueryRepository.php
<?php
/**
* Click Forum Software
*
* An open source forum software
*
* @package Click
* @author Braeden Dillon
* @version Version 1.0
* @since 23/11/2015
*/
//-------------------------------------------------------------------------- ------------
/**
* Query Repository Class
*
* @package Click
* @subpackage Libraries
* @category QueryRepository
* @author Braeden Dillon
*/
class QueryRepository {
private $_database;
function __construct() {
$this->_database = Database::getInstance();
}
function fetchClient($email, $password) {
$client = $this->_database->prepare('SELECT id, username, password FROM users WHERE email = :email && password = :password');
$client -> bindParam(':email', $email);
$client -> bindParam(':password', $password);
$client -> execute();
$client -> setFetchMode(PDO::FETCH_ASSOC);
return $client->fetch();
}
function insertClient($name, $username, $email, $password) {
$client = $this->_database->prepare('INSERT INTO user (name, username, email, password) VALUES (:name, :username, :email, :password)');
$client -> bindParam(':name', $name);
$client -> bindParam(':username', $username);
$client -> bindParam(':email', $email);
$client -> bindParam(':password', $password);
$client -> execute();
return true;
}
}
database.php中
<?php
/**
* Click Forum Software
*
* An open source forum software
*
* @package Click
* @author Braeden Dillon
* @version Version 1.0
* @since 23/11/2015
*/
//--------------------------------------------------------------------------------------
/**
* Database Class
*
* @package Click
* @subpackage Libraries
* @category Database
* @author Braeden Dillon
*/
class Database extends PDO {
private static $_instance = null;
private $_error;
function __construct() {
parent::__construct('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/database'), Config::get('mysql/username'), Config::get('mysql/password'));
try {
$this->setAttribute(PDO::ATTR_PERSISTENT, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if(!isset(self::$_instance)) {
self::$_instance = new Database();
}
return self::$_instance;
}
}
答案 0 :(得分:1)
我需要将查询设置为变量。谢谢你们!
$data = $QueryRepository->fetchClient($_POST['email'], $_POST['password']);
if($data != false || $data != null) {