Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP shows Array to string conversion when building an SQL query

ErrorWarning: Array to string conversion

Short 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.

  • Applies to PHP 8.3, PDO MySQL
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The failing query reads WHERE id IN (Array)The array of rows goes straight into the SQL stringFix
    2. 2WHERE id IN (?) returns nothing or one row even though several ids existA list is bound to one placeholderFix

What does the error look like?

  • Warning: Array to string conversion
  • Notice: 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

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.