catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

get() for a single field returns array instead of plain data #208

Closed Kasendwa closed 6 years ago

Kasendwa commented 9 years ago

In the documentation, we see calling the get() method while requesting one field will assign the field data directly to a variable. However, in Medoo Version: 0.9.8, it assigns it as an array and the data is stored in index 0.

This is what docs indicate.

$email = $database->get("account", "email", [ "user_id" => 1234 ]);

// $email = "foo@bar.com"

A closer look at the method (development version), reveals that the where parameter data is modified from Line 799 to 804 hence making Line 816 if no use.

See method below.

public function get($table, $join = null, $column = null, $where = null) { if (!isset($where)) { $where = array(); }

    $where['LIMIT'] = 1;

    $query = $this->query($this->select_context($table, $join, $column, $where));

    if ($query)
    {
        $data = $query->fetchAll(PDO::FETCH_ASSOC);

        if (isset($data[0]))
        {
            $column = $where == null ? $join : $column;

            if (is_string($column) && $column != '*')
            {
                return $data[ 0 ][ $column ];
            }

            return $data[ 0 ];
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
catfan commented 6 years ago

It's fixed.