2012-08-16 114 views
0

我想從使用Facebook Open Graph的數組中取數據。我正在使用foreach語句獲取數組,但是這使得6個不同的INSERT語句。我希望數組中的每個值能夠被提交到一個MYSQL行中。將變量數組數據插入MYSQL

請注意,$ insert2插入6條不同的行,$ insert 3插入一行,但只是$ urlform。

任何幫助將不勝感激。

的代碼是:

require_once('OpenGraph.php'); 
$urlget2 = "http://www.example.com/article/1/title"; 
$graph = OpenGraph::fetch($urlget2); 
foreach ($graph as $key => $value) { 
    $array = array($key => $value); 
print_r($array); 
    $key = type; 
    $key1 = title; 
    $key2 = image; 
    $key3 = description; 
    $key4 = url; 
    $subtype = $array[$key]; 
    $subtitle = $array[$key1]; 
    $subimage = $array[$key2]; 
    $subdesc = $array[$key3]; 
    $urlform = $array[$key4]; 
    $useridme = "5"; 
    $insert2 = "INSERT INTO share VALUES('','$urlform','$subtype','$subtitle','$subimage','$subdesc','$subtime','$datesubmit','$subevent','$useridme','$facebookidme','$grpurl')"; 
    echo "<hr />"; 
    echo $insert2; 
} 
echo "<hr />"; 
$insert3 = "INSERT INTO share VALUES('','$urlform1','$subtype','$subtitle','$subimage','$subdesc','$subtime','$datesubmit','$subevent','$useridme','$facebookidme','$grpurl')"; 
    echo "<hr />"; 
    echo $insert3; 
+0

您的'foreach'在哪裏完成?一些線路丟失。 – Jocelyn 2012-08-16 05:25:41

+0

Foreach在echo $ insert2之後完成。 – stevieD 2012-08-16 05:37:04

+0

對不起。我沒有仔細查看:-(此外,縮進有點誤導 – Jocelyn 2012-08-16 05:37:55

回答

2

用這個替換代碼。此外閱讀本link

require_once('OpenGraph.php'); 
$urlget2 = "http://www.example.com/article/1/title"; 
$graph = OpenGraph::fetch($urlget2); 
$insertvalues = '';  
foreach ($graph as $key => $value) { 
    $array = array($key => $value); 
    print_r($array); 
    $key = type; 
    $key1 = title; 
    $key2 = image; 
    $key3 = description; 
    $key4 = url; 
    $subtype = $array[$key]; 
    $subtitle = $array[$key1]; 
    $subimage = $array[$key2]; 
    $subdesc = $array[$key3]; 
    $urlform = $array[$key4]; 
    $useridme = "5"; 

//get all insert values 
    $insertvalues .= ", ('','".$urlform."','".$subtype."','".$subtitle."','".$subimage."' ,'".$subdesc."','".$subtime."','".$datesubmit."','".$subevent."','".$useridme."','".$facebookidme."','".$grpurl."')"; 

    echo "<hr />"; 
    echo $insertvalues; 
} 
echo "<hr />"; 
$insertvalues= substr($insertvalues,1); //just takes off the leading comma 

$insert3 = "INSERT INTO share VALUES ".$insertvalues; 
echo "<hr />"; 
echo $insert3; 
mysql_query($insert3); 
+0

嘗試使用這個,但它仍然提交了6行到數據庫。有沒有辦法在foreach之後回顯數組中的各個值? – stevieD 2012-08-16 20:28:13

+0

現在在此代碼之後使用更新和刪除不需要的行結束使用解決方法。 – stevieD 2012-08-17 05:11:34

0

foreach循環運行在$insert2連連。 我會改變:

$insert2 = "INSERT INTO share VALUES('','$urlform','$subtype', 
      '$subtitle','$subimage','$subdesc','$subtime','$datesubmit', 
      '$subevent','$useridme','$facebookidme','$grpurl')"; 

到:

$inserts[] = "INSERT INTO share VALUES('','$urlform','$subtype', 
       '$subtitle','$subimage','$subdesc','$subtime','$datesubmit', 
       '$subevent','$useridme','$facebookidme','$grpurl')"; 

使得foreach循環之後你就會有$inserts陣列。您將能夠巴使用,如果爲:

foreach($inserts as $insert){ 
    //use $insert to insert the record into the DB 
} 

對於喬斯林:
我沒有看到使用一個連接字符串,一個數組的優勢,但這裏有雲:
你可以改變$insert2 = ...
至:

$inserts .= "INSERT INTO share VALUES('','$urlform','$subtype', 
        '$subtitle','$subimage','$subdesc','$subtime','$datesubmit', 
        '$subevent','$useridme','$facebookidme','$grpurl');"; 
+0

OP問到的是:「我希望數組中的每個值都能夠被提交到一個MYSQL行。」使用你的代碼將獲得許多單獨的查詢而不是單個查詢 – Jocelyn 2012-08-16 05:43:23

+0

@Jocelyn np,請參閱更新後的答案 – alfasin 2012-08-16 05:47:08

+0

區別在於,單個INSERT比許多單獨的INSERT更快,如果要在腳本中插入10條記錄,則無關緊要。但是,如果你想快速插入數千條記錄,限制INSERT查詢的數量會加快執行速度。 – Jocelyn 2012-08-16 05:50:29

0

我建議嘗試此代碼:現在

require_once('OpenGraph.php'); 
$urlget2 = "http://www.example.com/article/1/title"; 
$graph = OpenGraph::fetch($urlget2); 
$insert2 = array(); 
foreach ($graph as $key => $value) { 
    $array = array($key => $value); 
    print_r($array); 
    $key = type; 
    $key1 = title; 
    $key2 = image; 
    $key3 = description; 
    $key4 = url; 
    $subtype = $array[$key]; 
    $subtitle = $array[$key1]; 
    $subimage = $array[$key2]; 
    $subdesc = $array[$key3]; 
    $urlform = $array[$key4]; 
    $useridme = "5"; 
    $insert2[] = "('','$urlform','$subtype','$subtitle','$subimage','$subdesc','$subtime','$datesubmit','$subevent','$useridme','$facebookidme','$grpurl')"; 
    echo "<hr />"; 
} 
$query = "INSERT INTO share VALUES".implode(", ", $insert2); 
echo "$query<br>"; 
mysql_query($query); 

echo "<hr />"; 
$insert3 = "INSERT INTO share VALUES('','$urlform1','$subtype','$subtitle','$subimage','$subdesc','$subtime','$datesubmit','$subevent','$useridme','$facebookidme','$grpurl')"; 
echo "<hr />"; 
echo $insert3; 

$ insert2是將存儲所有你要插入的值的數組。在foreach循環之後,將所有值連接在一起以創建將插入表中所有值的單個 INSERT查詢。使用一個INSERT查詢插入所有值比使用多個單獨的插入要快得多。如果您的腳本同時插入許多記錄(數千或更多),您可能會注意到它現在執行得更快。

文檔:INSERT

+0

嘗試了這一點,它沒有插入$查詢到數據庫中。 $查詢的回聲是:INSERT INTO共享VALUESINSERT INTO共享VALUES('','','','RG3和紅皮在季前賽開局中擊敗賬單 - Yoozpaper |免費在線文章','','','1345156569' ,「2012-08-16」,「」,「5」,「47001227」,「人蔘皁甙Rg3和 - 紅皮敗國債功能於季前賽揭幕戰--- yoozpaper- | - 免費 - 在線文章」) ,INSERT INTO share VALUES('','','','','','','1345156569','2012-08-16','','5','47001227','') ,INSERT INTO share VALUES('','','article','','','','1345156569','2012-08-16','','5','47001227','' ) – stevieD 2012-08-16 22:39:09