0

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;
}
}
4
  • perhaps you intended to inspect print_r($QueryRepository)->fetchClient($_POST['email'], $_POST['password'])); instead ? Commented Nov 26, 2015 at 12:24
  • @Calimero that's not even proper syntax, thanks though. Commented Nov 26, 2015 at 12:39
  • 2
    Your fetchClient() function have a return statement, but when you call the function, you aren't associating this return with a variable. Follow the @Calimero advice and then $var = $QueryRepository->fetchClient($_POST['email'], $_POST['password']); print_r($var); Commented Nov 26, 2015 at 13:16
  • You were correct, I can't believe I missed it. Commented Nov 26, 2015 at 13:42

1 Answer 1

1

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

$data = $QueryRepository->fetchClient($_POST['email'], $_POST['password']);
if($data != false || $data != null) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.