PHP PDO同时保存一个字符串和数组

时间:2019-01-20 01:16:15

标签: php pdo

有人可以给我建议吗,如何使用PDO执行跟进查询? Status是具有单个值“ 1”的字符串,$ array具有许多不同的值。 我的查询应创建一个如下表。

status  | values
   1    |   1
   1    |   2
   1    |   3
   1    |   4
   1    |   5

我的代码。

$array = [1,2,3,4,5];

$status = '1';

# This example not work.

$stmt->prepare("INSERT INTO table (status, array) VALUES (:status, :array)");
$stmt->bindParam(':status', $status);
$stmt->bindParam(':array', $array);
return $stmt->execute();


# I had tried to do it like follow example.
# But it not work.

$stmt->prepare("INSERT INTO table (status, array) VALUES (:status, :array)");
 foreach($array as $value){
  $stmt->bindParam(':status', $status);
  $stmt->bindParam(':array', $value);
  return $stmt->execute();
}


# This example work, but it looks a little strange.

foreach($array as $value){
$stmt->prepare("INSERT INTO table (status, array) VALUES (:status, :array)");
    $stmt->bindParam(':status', $status);
    $stmt->bindParam(':array', $value);
    return $stmt->execute();
}

有人有更好的主意吗?

0 个答案:

没有答案