Welcome Guest, Not a member yet? Register   Sign In
$this->db->result_array() won't return, but will print via print_r
#1

Hello,

I'm trying to pass data from a method -
Code:
// Start the query
        $this->connection->select('*');
        $this->connection->from($this->itemdb);

        if(is_numeric($item))
        {
            $this->connection->where(ROcolumn($this->itemdb,'id',false,$serverId),$item);
        }
        else
        {
            $this->connection->like(ROcolumn($this->itemdb,'name_japanese',false,$serverId),''.$item.'');
            $this->connection->order_by(ROcolumn($this->itemdb, 'id', false, $serverId),'ASC');
            $this->connection->limit(50, 0); // limit if more than 1.
        }

        $query = $this->connection->get();

        error_log($this->connection->last_query());
        error_log(print_r($query->result_array(),true));

        if($this->connection->_error_message())
        {
            //error_log($this->connection->_error_message());
            die($this->connection->_error_message());
        }

        if($query->num_rows() > 0)
        {
            $row = $query->result_array();
            $query->free_result();
            return $row;
        }
        else {
            return false;
        }

Here $row returns as empty for certain queries that execute successfully with an array of data that can print in my error_log.

Example when it selects database for $item = 4147 , but it doesn't return the array. This happens with queries with this particular value or certain other strings. Below you see 4147 exists and is stored but the method will return as empty or false for some odd reason.
Code:
[Fri Jul 17 23:50:22.466354 2015] [:error] [pid 684] [client ::1:51705] SELECT *\nFROM (`item_db`)\nWHERE `id` =  '4147', referer: http://localhost/fusion/rodb
[Fri Jul 17 23:50:22.466454 2015] [:error] [pid 684] [client ::1:51705] Array\n(\n    [0] => Array\n        (\n            [id] => 4147\n            [name_english] => Baphomet_Card\n            [name_japanese] => Baphomet Card\n            [type] => 6\n            [price_buy] => 20\n            [price_sell] => 10\n            [weight] => 10\n            [atk] => 0\n            [matk] => 0\n            [defence] => 0\n            [range] => 0\n            [slots] => 0\n            [equip_jobs] => 4294967295\n            [equip_upper] => 63\n            [equip_genders] => 2\n            [equip_locations] => 2\n            [weapon_level] => 0\n            [equip_level_min] => 0\n            [equip_level_max] => \n            [refineable] => 0\n            [view] => 0\n            [bindonequip] => 0\n            [script] => bonus bHit,-10; bonus bSplashRange,1;\n            [equip_script] => \n            [unequip_script] => \n        )\n\n)\n, referer: http://localhost/fusion/rodb
Reply
#2

(This post was last modified: 07-18-2015, 02:17 PM by Wouter60.)

If you expect only one row from your database, use $this->query->row_array() instead of result_array().
result_array() always returns an array with one or more rows.
To get the first row from result_array() , use this:
PHP Code:
$records $this->query->result_array();
$first_record $records[0]; 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB