2016-04-15 8 views
1

私はa,b,c,d,eの形でhashのシリーズを持っていると私は、次の配列から複数のmySQL挿入を作成するには?

$hashes = array(); 
while(($row = mysql_fetch_assoc($sql))) { 
    $hashes[] = $row['hash']; 
} 

$_SESSION['hashes'] = implode(',', $hashes); // a,b,c,d,e 

を通じてそれを行う私の質問は、私は以下のように複数のインサートを追加する方法ですか?

INSERT INTO alerts_data (alerts_data_id, alerts_data_hash) 
VALUES 
('$last insert id', 'hash 1'), 
('$last insert id', 'hash 2') 
('$last insert id', 'hash 3') 

答えて

0

MySQLiでPrepared Statementsを使用し、挿入クエリをループして複数のレコードを挿入します。

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "myDB"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 


// prepare and bind 
$stmt->prepare("INSERT INTO alerts_data (alerts_data_id, alerts_data_hash) VALUES (?, ?)"); 

$hashes = array(); 
while(($row = mysql_fetch_assoc($sql))) { 
    $hashes[] = $row['hash']; 
} 


foreach($hashes as $key => $hashe) 
{ 
    $stmt->bind_param($key, $hashe['hash']); 
    $stmt->execute(); 
} 
$stmt->close(); 
$conn->close(); 
?> 
関連する問題