0

I am trying to take data from an array using Facebook Open Graph. I am getting the array using a foreach statement, but that is making 6 different INSERT statements. I would like each value from the array to be able to be submitted into one MYSQL line.

Note that $insert2 is inserting 6 different lines and $insert 3 is inserting one line, but just $urlform.

Any help would be greatly appreciated.

The code is:

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;
stevieD
  • 135
  • 2
  • 5
  • 14

3 Answers3

2

Replace code with this one. Also read this 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);
Community
  • 1
  • 1
Vinay
  • 2,564
  • 4
  • 26
  • 35
  • Tried using this, but it still submitted 6 rows into the db. Is there a way to echo out the individual values in the array after the foreach? – stevieD Aug 16 '12 at 20:28
  • Ended up using a workaround after this code with Updates and Deleting Unnecessary Rows for now. – stevieD Aug 17 '12 at 05:11
0

The foreach loop is running over $insert2 again and again. I would change:

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

to:

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

so that after the foreach loop you'll have an array of $inserts. You'll ba able to use if as:

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

For Jocelyn:
I don't see an advantage in using a concatenated string over an array, but here it goes:
you can change $insert2 = ...
to:

$inserts .= "INSERT INTO share VALUES('','$urlform','$subtype', 
                  '$subtitle','$subimage','$subdesc','$subtime','$datesubmit',
                  '$subevent','$useridme','$facebookidme','$grpurl');";
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • What the OP asked was: "I would like each value from the array to be able to be submitted into one MYSQL line." With your code you will get many separate queries instead of one single query. – Jocelyn Aug 16 '12 at 05:43
  • The difference is that a single INSERT is faster than many separate INSERTs. It doesn't matter much if you are inserting 10 records in your script. However if you want to quickly insert thousands of records, limiting the number of INSERT queries will speed up execution a lot. – Jocelyn Aug 16 '12 at 05:50
  • If you're using a `PreparedStatement` with the same `INSERT` and just change the value of the parameters - it should be quite efficient. – Nir Alfasi Aug 16 '12 at 06:11
0

I suggest trying this code:

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 is now an array that will store all the values you want to insert. After the foreach loop, all the values are concatenated to create a single INSERT query that will insert all values in the table. Using one INSERT query to insert all values is much faster than using many separate inserts. If your script inserts many records (thousands or more) at the same time, you will likely notice it now executes faster.

Documentation: INSERT

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
  • Tried this and it did not insert $query into the database. The echo of $query was: INSERT INTO share VALUESINSERT INTO share VALUES('','','','RG3 And Redskins Defeat Bills In Preseason Opener - Yoozpaper | Free Online Articles','','','1345156569','2012-08-16','','5','47001227','rg3-and-redskins-defeat-bills-in-preseason-opener---yoozpaper-|-free-online-articles'), INSERT INTO share VALUES('','','','','','','1345156569','2012-08-16','','5','47001227',''), INSERT INTO share VALUES('','','article','','','','1345156569','2012-08-16','','5','47001227','') – stevieD Aug 16 '12 at 22:39