1

How do I bind an array WITHOUT a key (non associative array) to a PDO SQL statement?

Say I want to insert all 3 entries from $fruits to the database:

$fruits = array(
 array('apple','red','sweet'),
 array('lemon','yellow','bitter'),
 array('bannana','yellow','sweet')
);

$stmt = $pdo->prepare("INSERT INTO `fruits` (`name`, `color`, `taste`) VALUES ( ???? )");

$stmt->bindValue(??);

$stmt->execute();

I know it can be done with associative array (Binding multiple values in pdo) but in this scenario The array have no key.

Community
  • 1
  • 1
Azevedo
  • 2,059
  • 6
  • 34
  • 52

1 Answers1

0

Just put it inside a loop...

$fruits = array(
  array('apple','red','sweet'),
  array('lemon','yellow','bitter'),
  array('bannana','yellow','sweet')
);

$stmt = $pdo->prepare("INSERT INTO `fruits` (`name`, `color`, `taste`) VALUES (?, ?, ?, ?)");

foreach ($fruits as $row) {
  $stmt->bindValue($row);
}

$stmt->execute();
vsmoraes
  • 194
  • 2
  • 7