0

我正在尝试使用 Facebook Open Graph 从数组中获取数据。我正在使用 foreach 语句获取数组,但这会生成 6 个不同的 INSERT 语句。我希望数组中的每个值都能够提交到一个 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;
4

3 回答 3

2

用这个替换代码。另请阅读此链接

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);
于 2012-08-16T05:46:15.687 回答
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. 你可以使用 if 为:

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

对于 Jocelyn:
我看不出在数组上使用连接字符串有什么优势,但是在这里:
您可以更改$insert2 = ...
为:

$inserts .= "INSERT INTO share VALUES('','$urlform','$subtype', 
                  '$subtitle','$subimage','$subdesc','$subtime','$datesubmit',
                  '$subevent','$useridme','$facebookidme','$grpurl');";
于 2012-08-16T05:40:35.767 回答
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 查询来插入所有值比使用许多单独的插入要快得多。如果您的脚本同时插入许多记录(数千条或更多),您可能会注意到它现在执行得更快。

文档:插入

于 2012-08-16T05:58:35.897 回答