PHP shows Array to string conversion when building an SQL query
Warning: Array to string conversionShort answer
PHP raises Array to string conversion when an array is placed inside a string, and the query text then contains the word Array. Turn the rows into a flat list of ids, then add one placeholder per id and pass the ids to execute. Do not paste the list into the SQL text.
Fix it fast
1Find out which cause you have
Dump the variable you put into the query. If it shows an array of rows, not of plain ids, that is the cause.
var_dump($ids);2Match what you see, then jump to its fix
What does the error look like?
Warning: Array to string conversionNotice: Array to string conversion
Cause 1 of 2
The array of rows goes straight into the SQL string
1Check
Fetching rows returns an array per row, so interpolating the result puts the word Array into the text. PHP 8 reports this as a warning, where older versions gave a notice.
$sql = "SELECT * FROM orders WHERE user_id IN ($rows)";
echo $sql;2Fix
Pull the id out of each row first, so you hold a flat array.
$ids = array_column($rows, 'id');3Verify
var_dump shows a flat list of numbers.
var_dump($ids);Confirmed by php.net
Cause 2 of 2
A list is bound to one placeholder
1Check
A placeholder takes the place of one whole value, so an array cannot fill an IN list through a single question mark.
echo $sql;2Fix
Build one question mark per id and pass the ids to execute.
$in = implode(',', array_fill(0, count($ids), '?'));
$stmt = $pdo->prepare("SELECT * FROM orders WHERE user_id IN ($in)");
$stmt->execute($ids);
$orders = $stmt->fetchAll();3Verify
The result holds the rows for every id in the list.
var_dump(count($orders));Confirmed by php.net
How do I stop it coming back?
- Use prepared statements with generated placeholders, never string interpolation.
- Call var_dump on the value before it enters a query while you build it.
- Run PHP 8 in development so this warning is not hidden.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fix