sammy001 sammy001 - 4 months ago 6
MySQL Question

fetch data from array and save it in database

I had this code

echo "<pre>";
print_r($data);
echo "</pre>";


through which i got the following data

Spreadsheet_Excel_Reader Object
(
[sheets] => Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 2
[numCols] => 5
[cells] => Array
(
[1] => Array
(
[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location
)
)
)
)
)


I needed numCols so i used the following code

echo $data->sheets[0]['numCols'];


and got the result
5


Now i wish to fetch the following values

[1] => sno
[2] => candidta name
[3] => email
[4] => ctc
[5] => location


and store them in the table like this

id value
1 sno
2 candidta name
3 email
4 ctc
5 location


Can anyone please tell how it can be done

Answer
$newarray = $data->sheets[0]['cells'][1];
for ( $i=1; $i < count($newarray); $i++)
    { 
        echo $newarray[$i];

        $sql= "INSERT INTO `tablename` (value) VALUES ('$newarray[$i]')";
        if(!mysqli_query($con,$sql))
            {
                echo "Error: " . $sql. "<br>" . mysqli_error($con);
            }  
    } 
Comments