 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
Babe Ruth
Joined: 18 Apr 2008 Posts: 6
|
Posted: Fri Apr 18, 2008 9:43 am Post subject: Problems in Transforming MySQL Query Results to Spreadsheet |
|
|
I am trying to transform an SQL result set into an Excel spreadsheet. I want to turn all the records from the table into rows of an Excel spreadsheet. I am using PEAR's Spreadsheet_Excel_Writer so that I can make a format/template design of a spreadsheet. Take a look at the code I made:
<?php
// include class file
include 'Spreadsheet/Excel/Writer.php';
// initialize reader object
$excel = new Spreadsheet_Excel_Writer();
// send client headers
$excel->send('country.xls');
// add worksheet
$sheet =& $excel->addWorksheet('SQL_Output');
// attempt a connection
try {
$pdo = new PDO('mysql:dbname=cdms;host=localhost', 'root', 'admin');
} catch (PDOException $e) {
die("ERROR: Could not connect: " . $e->getMessage());
}
// read data from database
// convert into spreadsheet
$rowCount = 0;
$sql = "SELECT * FROM businessunits";
if ($result = $pdo->query($sql)) {
// get header row
for ($x=0; $x<$result->columnCount(); $x++) {
$meta = $result->getColumnMeta($x);
$sheet->write($rowCount, $x, $meta['name']);
}
// get data rows
$rowCount++;
while($row = $result->fetch()) {
foreach ($row as $key => $value) {
$sheet->write($rowCount, $key, $value);
}
$rowCount++;
}
} else {
echo "ERROR: Could not execute $sql. " . print_r($pdo->errorInfo());
}
// close connection
unset($pdo);
// save file to disk
if ($excel->close() === true) {
echo 'Spreadsheet successfully saved!';
} else {
echo 'ERROR: Could not save spreadsheet.';
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
The problem I am encountering is that the value of the SQL results doesn't match with what the results show in the Excel. Instead of showing the correct results for the first column, it always show what are the query results of my last column. The sample is this:
UnitID SOLID RCNumber GroupNumber
0 9999 999 0
1 1901 270 1
1 1902 271 1
1 1901 274 1
3 1904 275 3
1 3401 276 1
What lines should I change in my php code in order for the excel results match what is in my database |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Fri Apr 18, 2008 11:35 am Post subject: |
|
|
How about adding a after opening the foreach() loop? This will show you the values, and you'll see whether the column values (= keys of your array) are the right ones.
BTW, instead of fetching the values yourself and instead of writing the file yourself, you might want to take a look at Structures_DataGrid and its subpackages SDG_DataSource_PDO and SDG_Renderer_XLS. This combination could generate your Excel files in just a few lines of code. |
|
| Back to top |
|
 |
Babe Ruth
Joined: 18 Apr 2008 Posts: 6
|
Posted: Fri Apr 18, 2008 1:58 pm Post subject: about var_dump($row) |
|
|
Thanks for your reply. I did not expect a reply this early. Now when I run with the additional code ---var_dump($row); --- it is concluded from my script that for each row, the first column always takes the values of my last column.
I know there is a problem with my script or how do I conceptualize it.
Can anyone teach me a better code to put the sql results into the spreadsheet, but in a manner that I have the flexibility to put the results to whatever starting spreadsheet-cell that I want. For example, I would like the query results to be located not from cells 'A1 to J25' but from cells 'B2 to K26'
BTW, i have noted the Structures_DataGrid |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1011
|
Posted: Fri Apr 18, 2008 2:52 pm Post subject: Re: about var_dump($row) |
|
|
| Babe Ruth wrote: | | Can anyone teach me a better code to put the sql results into the spreadsheet, but in a manner that I have the flexibility to put the results to whatever starting spreadsheet-cell that I want. For example, I would like the query results to be located not from cells 'A1 to J25' but from cells 'B2 to K26' |
Well, currently you're using just your array keys for specifying the column (second parameter of the write() method). What you need is a second variable like your existing $rowCount variable, maybe $colCount. Initialize it with 1 (for the second column) directly before the foreach() loop (but in the while() loop). This ensures that each row starts at the second column. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|