1

I want to export sql query results as table on an XLS file (or anything that can be opened as table on ms excel)

I've tried this one » PHP code to convert a MySQL query to CSV

however, it removes any leading zeros on the result

how do I maintain/preserve the leading zeros?

Community
  • 1
  • 1
skeith
  • 63
  • 1
  • 3
  • 10

3 Answers3

1

Suppose You want to set telephone no with leading "0" when export to CSV by PHP MySql query, Just write in result data as below

'="'.$result['telephone'].'"'
Prasant Kumar
  • 668
  • 8
  • 12
1

Add a single quote before the first zero, like that: '002

That mean you should maybe define which columns are text and which are numbers, and force every text columns to begin witha single quote. A number with 0 prefix is not a number but a text in this way.

regilero
  • 27,883
  • 6
  • 54
  • 94
  • how do I define column type without adding single quote as I can not/should not modify the data on the mysql database. In addition to that condition, the results of the query is not always has leading zeros, only some of them has it – skeith Aug 06 '11 at 12:01
  • I mean add the quote on the output, not on the database, into the while-foreach. Define an array for your columns definitions, check that the foreach loop is on that column, then prefix the output: `$value = '"' . "'" . $value . '"' . "\t";` – regilero Aug 06 '11 at 12:05
  • okay, I'll try it. one more question what is a \t ? where can I find reference on that kind of strings? – skeith Aug 06 '11 at 13:22
  • it's a tabulation, search for special characters escaping on php doc – regilero Aug 06 '11 at 15:00
0

have you seen this export lib which use a building block method like Google AppInventor?

http://www.freegroup.de/software/phpBlocks/demo.html

Andreas
  • 11
  • 1