What is the reason for Trying to get property of non-object in?

0

I'm trying to return bank data using this code:

//avoid Undefined variable
$errors = [];

if (Input::exists('post'))
{
    $validate = new Validate;

    $validation = $validate->check($_POST, array(
        'username' => array(
            'required' => true
        ),

        'password' => array(
            'required' => true
        )
    ));


    if ($validation->passed())
    {

        $data = $this->_model->get_member_hash(Input::get('username'));

        if (Password::password_verify(Input::get('password'), $data[0]->admin_password))
        {
            echo 'correct data';
        }
        else
        {
            echo 'incorrect data';
        }
    }
    else
    {
        $errors = $validation->errors();
    }

Only he's returning these two errors to me

  

Notice: Undefined offset: 0

     

Notice: Trying to get property of non-object in

What reason?

My model:

public function get_member_hash($username)
    {
        return $this->_db->read('SELECT * FROM ' . DATABASE_PREFIX . 'admins WHERE admin_email=:email OR admin_username=:username', array(
            ':email'    => $username,
            ':username' => $username,
        ));
    }

Validation class

<?php

/**
 * 
 */
class Input
{

    /**
     * 
     */
    public static function exists($type = 'post')
    {
        switch ($type)
        {
            case 'post':
                return (!empty($_POST)) ? true : false;
                break;

            case 'get':
                return (!empty($_GET)) ? true : false;
                break;

            default :
                return false;
                break;
        }
    }

    /**
     * 
     */
    public static function get($item)
    {
        if (isset($_POST[$item]))
        {
            return trim(strip_tags(filter_input(INPUT_POST, $item)));
        }
        else if (isset($_GET[$item]))
        {
            return trim(strip_tags(filter_input(INPUT_GET, $item)));
        }

        //By default return string
        return '';
    }
}
    
asked by anonymous 12.05.2016 / 20:56

1 answer

-1

I do not know what this method returns:

public function get_member_hash($username) { return $this->_db->read('SELECT * FROM ' . DATABASE_PREFIX . 'admins WHERE admin_email=:email OR admin_username=:username', array( ':email' => $username, ':username' => $username, )); }

In your code you treat as if you were sure that it returns an array when you call $ data [0]

The problem is that this method is not returning an array, as I believe the query did not return a value.

Before calling $ data [0] you need to do a check, type: if(is_array($data){ //faz algo } else { //não retornou nenhum valor }

    
27.03.2017 / 02:05