2013-07-17 48 views
0

我有一個文本文件,誰的價值我已經投入陣列, 這是PHP代碼:插入陣列的值到MySQL

<?php 
    $homepage = file_get_contents('hourlydump.txt'); 
    $x = explode('|', $homepage); 
    $desc = array(); 
    $cat = array(); 
    $link = array(); 
    $m = 1; 
    $n = 2; 
    $p = 3; 

    for ($i = 1; $i <= count($x)/4; $i++) { 
     $m = $m + 4; 
     $desc[] = $x[$m]; 
     $n = $n + 4; 
     $cat[] = $x[$n]; 
     $p = $p + 4; 
     if ($x[$p]) 
      $link[] = $x[$p]; 
    } 
    echo "<pre>"; 
    print_r($desc); 
    print_r($cat); 
    print_r($link); 
?> 

輸出是這樣的:

Array 
(
    [0] => Kamal Heer - Facebook Official Video 720p Dual Audio [Hindi + Punjabi]76 mb by rANA.mkv 
    [1] => 50 HD Game Wallpapers Pack- 1 
) 
Array 
(
    [0] => Movies 
    [1] => Other 
) 
Array 
(
    [0] => http://kickass.to/kamal-heer-facebook-official-video-720p-dual-audio-hindi-punjabi-76-mb-by-rana-mkv-t7613070.html 
    [1] => http://kickass.to/50-hd-game-wallpapers-pack-1-t7613071.html 
) 
// 
// 
// 

請人幫我我不知道如何將這三個數組的值$ desc,$ cat和$ link 插入到mysql表中,列名爲description,category,link

我知道si多插入查詢,但不知道如何處理這些數組。

+0

請參閱此鏈接它可能幫助你http://stackoverflow.com/questions/7818570/inserting-multiple-array-values-in-mysql-database – Bharu

回答

0

下面是一個簡單的示例來讀取你的文件是從您檢索它的網站,以及它插入到數據庫消毒的數據:

<?php 
// fill with your data 
$db_host = 'localhost'; 
$db_user = ''; 
$db_pass = ''; 
$db_name = ''; 
$db_table = 'myTable'; 

$file = "hourlydump.txt.gz"; 
if($filehandle = gzopen($file, "r")) 
{ 
    $content = gzread($filehandle, filesize($file)); 
    gzclose($file); 
} 
else 
    die('Could not read the file: ' . $file); 

$con = mysqli_connect($db_host,$db_user,$db_pass,$db_name); 
if($con->connect_error) 
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error()); 

$sql = "INSERT INTO $db_table (description, category, link) VALUES (?, ?, ?)"; 
if (!$insert = $con->prepare($sql)) 
    die('Query failed: (' . $con->errno . ') ' . $con->error); 

foreach (explode("\n", $content) as $line) 
{ 
    list($md5hash,$desc,$cat,$link,$torrent) = explode("|", $line); 
    if (!$insert->bind_param('sss',$desc,$cat,$link)) 
     echo 'Binding parameters failed: (', $insert->errno, ') ', $insert->error; 

    if (!$insert->execute()) 
     echo 'Insert Error ', $insert->error; 
} 

$insert->close(); 
$con->close(); 

注意:您可能要檢查,如果該文件被加載成功,如果從田間地頭的爆炸存在與否,以防止進一步的問題,但總的來說這應該只是罰款。

此外,您可能還想更改$sql以反映您的MySQL表格以及頂部的$db_table

UPDATE:插入所有的值更改此:

$sql = "INSERT INTO $db_table (description, category, link) VALUES (?, ?, ?)"; 

要:

$sql = "INSERT INTO $db_table (md5, description, category, link, torrent) VALUES (?, ?, ?, ? ,?)"; 

這:

if (!$insert->bind_param('sss',$desc,$cat,$link)) 

要:

if (!$insert->bind_param('sssss',$md5hash,$desc,$cat,$link,$torrent)) 

注意s每個項目上面你需要一個s你有5個項目,以5 s的的S表示字符串,d雙,我整數,b BLOB you can read more at about it here.

還要注意$sql因爲我們將使用每個項目在bind_param我們有一個?

+0

非常感謝... 完美工作...沒有任何錯誤:) –

+0

還有一件事... 此代碼工作正常...爲插入這三個值... 但當我試圖插入所有五個值...它顯示錯誤爲 查詢失敗:(1136)列計數與第1行的值計數不匹配 任何想法...? –

+0

@YugeshMLuv查看更新,以插入所有5. – Prix

4

我會舉例說明如何進行基本的數據庫連接並完成插入操作,這僅用於說明目的。您應該在類中重組此代碼,以便每個插入語句都不會創建PDO對象,而是重新使用之前創建的對象。

function insertItem($desc, $cat, $link) { 
    $dbh = new PDO("mysql:host=host;dbname=db", $user, $pass); 
    $sql = "INSERT INTO table (description, category, link) VALUES (:desc, :cat, :link)"; 

    $sth = $dbh->prepare($sql); 
    $sth->bindValue(":desc", $desc); 
    $sth->bindValue(":cat", $cat);   
    $sth->bindValue(":link", $link); 
    $sth->execute(); 
    } 
+0

現在,它是如何完成的。 – Orangepill

+0

** + 1 **不易受SQL注入攻擊。 – michaelb958

1

您可以使用for語句。

for($x =0, $num = count($desc); $x < $num; $x++){ 
    // build you query 
    $sql = "INSERT into your_table (description, category, link) values ". 
      "(".$db->quote($desc[$x]).",".$db->quote($cat[$x]).",". 
       $db->quote($link[$x].")"; 
    $db->query($sql); 
} 

當然,您將不得不使用適合您所選數據庫api的衛生/引用方法。

-1

使數組爲字符串

$description = json_encode($desc); 
$category = json_encode($cat); 
$link = json_encode($link); 

然後將這些值與數據庫

在從字符串再次獲取

使用json_decode得到陣列的時間

0

嘗試這個。我假設只有這些多值都沒有插入

for($i = 0;$i<2;$++) { 
mysqli_query("INSER INTO tablename values(description,category,link) VALUES('$desc[$i]' 
,'$cat[$i]','$link[$i]')"); 
} 
0

你可以建立查詢,而你做你的計算:

$query = "INSERT INTO `table` (`description`, `category`, `link`) VALUES "; 
for ($i = 1; $i <= count($x)/4; $i++) { 
    $m = $m + 4; 
    $query .= "('".$x[$m]; 
    $n = $n + 4; 
    $query .= "','".$x[$n]; 
    $p = $p + 4; 
    if ($x[$p]) $query .= "','".$x[$p]."'),"; 
    else $query .= "',NULL),"; 
} 
$query = substr($query, 0, -1);//get rid of last comma 
mysqli_query($query); 

您也可以建立與查詢沿着陣列如果您需要:

$query = "INSERT INTO `table` (`description`, `category`, `link`) VALUES "; 
for ($i = 1; $i <= count($x)/4; $i++) { 
    $m = $m + 4; 
    $desc[] = $x[$m]; 
    $query .= "('".$x[$m]; 
    $n = $n + 4; 
    $cat[] = $x[$n]; 
    $query .= "','".$x[$n]; 
    $p = $p + 4; 
    if ($x[$p]){ 
     $link[] = $x[$n]; 
     $query .= "','".$x[$p]."'),"; 
    } else { 
     $link[] = $x[$n]; 
     else $query .= "',NULL),"; 
} 
$query = substr($query, 0, -1);//get rid of last comma 
mysqli_query($query);