I have fetched ids from account table as below:-var_dump($rows_i);
array(3) { [0]=> array(1) { ["id"]=> string(1) "2" } [1]=> array(1) { ["id"]=> string(1) "4" } [2]=> array(1) { ["id"]=> string(1) "5" } }
I am trying to fetch all debit amounts for the ids in the list per below SQL query:-
COALESCE((SELECT sum(amount) amnt FROM journal where debit_account_id in (".implode(",",$rows_i).")),0)
Am getting Warning and Fatal Error per below:
Warning: Array to string conversion in F:xampphtdocs…………….
Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘Array’ in ‘where clause’ in F:xampphtdocs………………
2
Answers
As has been pointed out in the comments:
Firstly,
$rows_i
is an array of arrays; you can extract the individualid
values usingarray_column
:You could then use that array inside the implode in your query.
PHP demo on 3v4l.org
Secondly, you could get all this data in one query. For example:
Example demo on dbfiddle
Check this out: