Quantcast
Channel: PHPExcel Forum Rss Feed
Viewing all articles
Browse latest Browse all 2707

New Post: From Excel (xls/xlsx) to MySql Database

$
0
0
Well... You need to have something like :
INSERT INTO YourTable (FieldUsername, FieldEmail, FieldGender, FieldCountry) VALUES ('psb', 'psb67@hotmail.com', 'M', 'India'), ('naga', 'naga@hotmail.com', 'F', 'France'), ...

In your loop (skip the first row, containing header), you build the values part :
if($SQL!='') $SQL.=',';
$SQL.='(\''.mysqli_real_escape_string($Link, $objWorksheet->getCellByColumnAndRow($colUsername, $row)->getValue()).'\',\''.
        mysqli_real_escape_string($Link, $objWorksheet->getCellByColumnAndRow($colEmail, $row)->getValue()).'\',\''.
        mysqli_real_escape_string($Link, $objWorksheet->getCellByColumnAndRow($colGender, $row)->getValue()).'\',\''.
        mysqli_real_escape_string($Link, $objWorksheet->getCellByColumnAndRow($colCountry, $row)->getValue()).'\')';
After the loop :
if($SQL!=''){
    $SQL='INSERT INTO YourTable (FieldUsername, FieldEmail, FieldGender, FieldCountry) VALUES '.$SQL;
    $result=mysqli_query($Link, $SQL);
}
$colUsername, $colEmail, .... contains the appropriate column number. $SQL, empty string on loop entry.
If you have numerous entries or the MySQL server don't accept big query, you need to check the size into the loop.
Adapt this if you use a cell iterator, add some checks on data(gender, email, ...), on result, use INSERT IGNORE or REPLACE...

My example uses not the "prepared statement" in the MySQL sense because I think that this is not the best solution here.

Viewing all articles
Browse latest Browse all 2707

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>