2015-09-26 257 views
0

我正在處理基於龐大數據的項目。我爲PHP中的每個數據創建了數組。現在我想將它插入到MySQL數據庫中。使用PHP將數組數據值插入MySQL數據庫表

<?php 
$servername = "localhost"; 
$username = "xxxx"; 
$password = "xxxxx"; 
$db_name = "xxxxxxx"; 

// Create connection 
$link = mysql_connect($servername, $username, $password); 

if(!$link){ 
echo "Failed" .mysql_connect_error(); 
} 

mysql_select_db($db_name); 

$data = array (
     [1] => array(
      'eng_title' => "john", 
      'content' => "john", 
      'category' => "john", 

     ), 
     [2] => array(
      'eng_title' => "john", 
      'content' => "john", 
      'category' => "john", 
     ), 
     [3] => array(
      'eng_title' => "john", 
      'content' => "john", 
      'category' => "john", 
     ), 
    ); 

if(is_array($data)){ 

    $sql = "INSERT INTO `table_a`(`eng_title`, `content`, `category`) values"; 

    $valuesArr = array(); 
    foreach($data as $row){ 

     $eng_title = mysql_real_escape_string($row['eng_title']); 
     $content = mysql_real_escape_string($row['content']); 
     $category = mysql_real_escape_string($row['category']); 


     $valuesArr[] = "('$eng_title', '$content', '$category')"; 
    } 

    $sql .= implode(',', $valuesArr); 
    echo $sql; 
    mysql_query($sql) or exit(mysql_error()); 

} 

?> 

這是我執行代碼時出現的消息。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 

我在其他程序中使用MySQLi,我想我正在使用最新版本的MySQL,這就是爲什麼上述程序無法正常工作。

+0

從$ sql中刪除'(當你執行echo時,你能給我們看什麼樣的$ sql) –

+0

在你的foreach中插入你的sql查詢。 – aldrin27

回答

0

正如我認爲你在「內爆」中錯過了一些東西。 試試這個 -

if(is_array($data)){ 

    $sql = "INSERT INTO `table_a`(`eng_title`, `content`, `category`) values"; 

    $valuesArr = array(); 
    $last=count($valuesArr); 
    $i=0; 
    foreach($data as $row){ 

     $eng_title = mysql_real_escape_string($row['eng_title']); 
     $content = mysql_real_escape_string($row['content']); 
     $category = mysql_real_escape_string($row['category']); 


     $sql .= "('$eng_title', '$content', '$category')"; 
     if($i<$last) 
      $sql .=" , "; 
    $i++; 
    } 


    echo $sql; 
    mysql_query($sql) or exit(mysql_error()); 

} 
+0

他需要「,」不能插入多行的空格 –

+0

謝謝@ELOUFIRHatim,現在糾正.. –

+0

請提醒一下,mysql_ *函數已被棄用多年。考慮使用mysqli_ *代替。 –

0

使用PDO最好,看看這裏:Choosnig an API

要與PDO執行INSERT語句,您可以到這裏看看更多的例子:PDO - Manual

所以對於你的情況,你可以用兩種方法做到:

1.在每個迴路上插入一行:

<?php 

$data = array (
    1 => array(
     'eng_title' => "john", 
     'content' => "john", 
     'category' => "john", 

    ), 
    2 => array(
     'eng_title' => "john", 
     'content' => "john", 
     'category' => "john", 
    ), 
    3 => array(
     'eng_title' => "john", 
     'content' => "john", 
     'category' => "john", 
    ), 
); 

$servername = "localhost"; 
$username = "xxxx"; 
$password = "xxxxx"; 
$db_name = "xxxxxxx"; 
$pdo = new PDO("mysql:host=$servername;dbname=$db_name", "$username", "$password"); 
foreach ($data as $key => $value) { 
    $eng_title = mysql_real_escape_string($value['eng_title']); 
    $content = mysql_real_escape_string($value['content']); 
    $category = mysql_real_escape_string($value['category']); 

    $query = $db->prepare('INSERT INTO table_a(eng_title, content, category) VALUES(:title, :content, :category)'); 
    $query->bindParam("title", $eng_title); 
    $query->bindParam("content", $content); 
    $query->bindParam("category", $category); 
    $query->execute(); 
} 

2.創建一個SQL語句並執行它:

<?php 

$data = array (
    1 => array(
     'eng_title' => "john", 
     'content' => "john", 
     'category' => "john", 

    ), 
    2 => array(
     'eng_title' => "john", 
     'content' => "john", 
     'category' => "john", 
    ), 
    3 => array(
     'eng_title' => "john", 
     'content' => "john", 
     'category' => "john", 
    ), 
); 

$servername = "localhost"; 
$username = "xxxx"; 
$password = "xxxxx"; 
$db_name = "xxxxxxx"; 
$pdo = new PDO("mysql:host=$servername;dbname=$db_name", "$username", "$password"); 

$query = "INSERT INTO table_a(eng_title, content, category) VALUES"; 

$counter = 0; 
$arraySize = sizeof($data); 
foreach ($data as $key => $value) { 
    $eng_title = mysql_real_escape_string($value['eng_title']); 
    $content = mysql_real_escape_string($value['content']); 
    $category = mysql_real_escape_string($value['category']); 

    $counter++; 
    $query .= "($eng_title, $content, $category)"; 
    if($counter < $arraySize) 
     $query .= ","; 
} 
$pdo->query($query); 

希望它會幫助你。