简体   繁体   English

使用PDO PHP查询MySQL数据库时出现令人困惑的错误

[英]Confusing error when I query my MySQL database with PDO PHP

So recently i've being working with PHP PDO, and I just decided to redevelop my entire database system. 所以最近我一直在使用PHP PDO,而我刚决定重新开发整个数据库系统。 So here's the problem, i've made a class called QueryRepository, and what it holds is a basic query from the database, with a connection to my database. 这就是问题所在,我制作了一个名为QueryRepository的类,它保存的是来自数据库的基本查询,并与我的数据库建立了连接。 When I call this query and I print out the output I recieve this is the output: 当我调用此查询并打印输出时,我收到的是输出:

QueryRepository Object ( [_database:QueryRepository:private] => Database Object ( [_error:Database:private] => ) ) {"success":1,"message":"Login Successfull!"} QueryRepository对象([_database:QueryRepository:private] =>数据库对象([_error:Database:private] =>)){“成功”:1,“消息”:“登录成功!”}

I don't understand, because it's a select query shouldn't I receive the table or false? 我不明白,因为这是一个选择查询,我不应该收到表格还是假的? Ignore the json. 忽略json。 Cheers guys, this is really weird to me. 干杯们,这对我来说真的很奇怪。

$QueryRepository = new QueryRepository();

$QueryRepository->fetchClient($_POST['email'], $_POST['password']);

print_r($QueryRepository);

This is where it all goes wrong ^ 这就是所有出错的地方^

Here are my files below. 这是我下面的文件。

index.php 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 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

<?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;
}
}

I needed to set the query to a variable. 我需要将查询设置为变量。 Thanks guys! 多谢你们!

$data = $QueryRepository->fetchClient($_POST['email'], $_POST['password']);
if($data != false || $data != null) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM