将可变数组数据插入MYSQL

时间:2012-08-16 05:20:09

标签: php mysql arrays facebook-graph-api foreach

我正在尝试使用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;

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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 as:

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');";

答案 2 :(得分: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