Seef Seef - 3 months ago 11
PHP Question

How to insert arrays into mysql using php

I am scraping data and then turning the data into arrays that output as follows:

array(8) {
[0]=> array(8) {
[1]=> string(1) "1"
[2]=> string(8) "Henricho"
[3]=> string(10) "Bruintjies"
[4]=> string(2) "23"
[5]=> string(3) "Agn"
[6]=> string(6) "10.17A"
[7]=> string(4) "-0.2"
[8]=> string(1) "8" }
[1]=> array(8) {
[1]=> string(1) "2"
[2]=> string(5) "Akani"
[3]=> string(7) "Simbine"
[4]=> string(2) "23"
[5]=> string(3) "Agn"
[6]=> string(6) "10.21A"
[7]=> string(4) "-0.2"
[8]=> string(1) "7" }


It is displayed by using
var_dump($results);


Is there a simple way to input the data into a sql table using $results?

Answer

You need to you something like:

foreach ($results as $value){  
    $allValues = implode('", "', $value); 
    //the above line will give us something like ("1", "Henricho"....etc)
    $query ='Insert into(col1, col2, col3, col4, col5, col6, col7, col8)  
         Values("'.$allValues.'")';
    //You can now execute your query
    echo $query."<BR />";
}    

Check Demo Here