आप तैयार बयान कोड का उपयोग कर निर्माण कर सकते हैं यहाँ उल्लेख किया,
PDO Prepared Inserts multiple rows in single query
पीएचपी तर्क की तरह तरह की हो जाएगा,
/**
* Insert With Ignore duplicates in Mysql DB.
*
* @param string $date Date.
*/
public static function insertWithIgnore($em, $container, $tableName, $fields, $rows)
{
$query = "INSERT IGNORE INTO $tableName (`" . implode('`,`', $fields) . "`) VALUES ";
$placeHolr = array_fill(0, count($fields), "?");
$qPart = array_fill(0, count($rows), "(" . implode(',', $placeHolr) . ")");
$query .= implode(",", $qPart);
$pdo = self::getPDOFromEm($em, $container);
$stmt = $pdo->prepare($query);
$i = 1;
foreach ($rows as $row) {
$row['created_at'] = date("Y-m-d H:i:s");
foreach ($fields as $f) {
if (!isset($row[$f])) {
$row[$f] = null;
}
$stmt->bindValue($i++, $row[$f]);
}
}
$result = $stmt->execute();
if ($result == false) {
$str = print_r($stmt->errorInfo(), true);
throw new \Exception($str);
}
$stmt->closeCursor();
$pdo = null;
}
/**
* Replace old rows in Mysql DB.
*
* @param string $date Date.
*/
public static function replace($em, $container, $tableName, $fields, $rows, $extraFieldValues = null)
{
if ($extraFieldValues != null) {
$fields = array_unique(array_merge($fields, array_keys($extraFieldValues)));
}
$query = "REPLACE INTO $tableName (`" . implode('`,`', $fields) . "`) VALUES ";
$placeHolr = array_fill(0, count($fields), "?");
$qPart = array_fill(0, count($rows), "(" . implode(',', $placeHolr) . ")");
$query .= implode(",", $qPart);
$pdo = self::getPDOFromEm($em, $container);
$stmt = $pdo->prepare($query);
$i = 1;
foreach ($rows as $row) {
if ($extraFieldValues != null) {
$row = array_merge($row, $extraFieldValues);
}
foreach ($fields as $f) {
$stmt->bindValue($i++, $row[$f]);
}
}
$stmt->execute();
if (!$stmt) {
throw new \Exception("PDO::errorInfo():" . print_r($stmt->errorInfo(), true));
}
$stmt->closeCursor();
$pdo = null;
}
आपका उदाहरण यह पहले से ही है मानकों की एक निश्चित संख्या है, इसलिए हल किया। क्या आप अपनी समस्या को थोड़ा और विस्तारित कर सकते हैं? अब तक तुमने क्या प्रयास किये हैं? इसे मूल रूप से वही काम करना चाहिए लेकिन अधिक पैरामीटर जोड़ना चाहिए, उदा। एक 'foreach' पाश में। – hakre
आप यहाँ उल्लेख किया कोड का उपयोग कर तैयार बयान फार्म कर सकते हैं http://stackoverflow.com/questions/1176352/pdo-prepared-inserts-multiple-rows-in-single-query –