I created an array using PHP and Microsoft office access database using this code
Code:
<?php try{
$sql = "SELECT * FROM mcc ";
$result = $conn->query($sql);
$row = $result->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
}catch(PDOExepction $e){
echo $e;
}?>
Output:
Array ( [0] => Array ( [sName] => Dihn [Name] => John Parker [BNE] => BOB DIHN ) )
Now I want to use this array for further operation like make an excel file and all but i cant because array contains []"Square brackets" ,"String without double quotation " ,"string with space" and no ","(comma) between two rows.
how can i simplify this code to use like normal array or any other suggestion.please help me
$row
is an array (as PHP tells you in the output of print_r
), so the array exists. print_r
just outputs a more human-readable form of the array, that's exactly what the function was designed for. If you don't want that, then you are using the wrong function.
If you want to process the array using PHP, don't echo it, but use it directly, e.g. like this:
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
// Rows is an array of the results
foreach($rows as $row) {
$thename = $row['Name'];
// Do something with the data
}
If you want to output the array in valid PHP syntax, the easiest solution is to replace print_r
with var_export
:
$row = $result->fetchAll(PDO::FETCH_ASSOC);
var_export($row);
You did not give the slightest hint about how you want to process the output. However, if you want to process the output of your script, it might be easier to use JSON:
echo json_encode($row);