Link to home
Start Free TrialLog in
Avatar of Lennart Ericson
Lennart EricsonFlag for Sweden

asked on

Why do I get double values?

Trying to rewrite a script that worked well with php 5.6 to get it to work with php 7.1.
It gives me database column titles OK but it doubles all values. Please help.
medlem_excel.php
medlem-2017-10-22.xls
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

I don't have the solution to your problem. However I have 2 remarks that might make your life easier.

1 - You take the painful route to generate your Excel file. Some other paths you might consider:
a - If you use it as a raw excel file... generate a simple csv file instead, eg
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM test_table;

Open in new window

(see https://dev.mysql.com/doc/refman/5.7/en/select-into.html) BUT in that case the file will be on the server, not the best solution in most cases
(Note that I used ";" instead of "," as a separator, since I presume your working language in Excel is not English)
b - you can generatethe csv file yourself directly, using the same parameters.

2 - Your code mixes lots of "xml-like" tags with php text. This makes your program highly difficult to undertsand / check / debug.
I would consider placing most of the tags in php $variables, and place these definitions in a separate php file to be included. Your program would then bereduced to 2 dozens of lines .
Good advice offered by fibo.  You would do well to rewrite this script.

Your double values are likely coming from the fact that you have a while() loop based on "$row = $ret2->fetch_array()", but your custom function mysqli_result() is also seeking on the same object, $ret2.  Your row-based loop will also tend to repeat the row closing tag.
Try replacing the nested while for $ret2 with a foreach also (much like $ret3)

while ($row = $ret2->fetch_array()) {
	foreach ($row as $k => $v) {
		echo "<ss:Cell ss:StyleID=\"s24\"><ss:Data ss:Type=\"String\">".$v."</ss:Data></ss:Cell>";
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
B-)
You're so right Julian!
Avatar of Lennart Ericson

ASKER

Thanks Julian. Your insight made my day!
You are welcome.
Thanks Julian! This is something that should probably be added to php.net's notes