简体   繁体   中英

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. 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. 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!"}

I don't understand, because it's a select query shouldn't I receive the table or false? Ignore the 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

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

I needed to set the query to a variable. Thanks guys!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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