I’m executing a query to a db2 ibm database in Laravel.
call STOREDPROC.GET('leaders', '{"params": ""}', 'lead_management', 'FR');
The problem is that the fetchAll() returns an array of stdClass object.
Array ( [0] => stdClass Object ( [noref] => 368 [nofab] => FORT [actionname] => FORT BP720 [longactionname] => Epargne fort [startat] => 1999-10-29 [endat] => 1999-10-29 [monthduration] => 0 [kam] => CTER [cls] => SDO [status] => O [deletebyte] => ) )
That’s nice and all, but I’d rather get an array instead. I have to send the data through a http request and unfortunately, the data received at the other end of the request returns as a stream.
So how do I make fetchAll return an array instead of an stdObject class ?
I have tried to edit the options in the database.php file in laravel as such:
'options' => [ PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_PERSISTENT => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ],
But it still gives me the exact same result.
This is my code to fetch the data in laravel:
private function execute($verb, $args): array { $query = $this->buildQueryString($verb, $args); $this->buildConnection(); $result = DB::connection('odbc')->select($query); return $result; }
Advertisement
Answer
It seems the problem was the encoding on special characters causing syntax errors and thus invalid formatted json. I have made it to work by converting to utf8 encoding and then a json_decode instead of encode.
public static function convert_from_latin1_to_utf8_recursively($result) { if (is_string($result)) { return utf8_encode($result); } elseif (is_array($result)) { $ret = []; foreach ($result as $i => $d) $ret[ $i ] = self::convert_from_latin1_to_utf8_recursively($d); return $ret; } elseif (is_object($result)) { foreach ($result as $i => $d) $result->$i = self::convert_from_latin1_to_utf8_recursively($d); return $result; } else { return $result; } }
And then in my response:
$response = json_decode($response, true); return $response;