rseigel Posted January 19, 2013 Share Posted January 19, 2013 Hi all, Here's the code: $result = mysql_query("SELECT product_supplier.product_supplier_reference , product.price , product.wholesale_price FROM product_attribute INNER JOIN product ON product.id_product = product_attribute.id_product AND product.active = '1' INNER JOIN product_supplier ON product_supplier.id_product = product_attribute.id_product UNION SELECT product_supplier.product_supplier_reference , product.price , product.wholesale_price FROM product_supplier INNER JOIN product ON product.id_product = product_supplier.id_product AND product.active = '1' AND product.cache_default_attribute = '0'"); $fetch = array(); while($row = mysql_fetch_array($result)){ $fetch[] = $row; } $list = $fetch; $fp = fopen('product_export.csv', 'w'); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); The SELECT works perfectly. It creates 3 columns. The code is supposed to save it as a csv with those 3 columns. Problem is it saves 6 columns (each one duplicated). I'm sure it's something silly and I just can't see it cause I've been staring at it too long. TIA Ron Quote Link to comment https://forums.phpfreaks.com/topic/273367-silly-array-issue/ Share on other sites More sharing options...
requinix Posted January 19, 2013 Share Posted January 19, 2013 mysql_fetch_array() returns the data duplicated: one array item for each numeric key (0, 1, 2) and one for each string key ("product_supplier_reference", "price", "wholesale_price"). Use mysql_fetch_assoc() to get just the string keys or mysql_fetch_row() to get just the numeric keys. Also, please try to switch to PDO or mysqli. Those two are more efficient, can make your code much safer, and offer more functionality. Quote Link to comment https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406985 Share on other sites More sharing options...
rseigel Posted January 19, 2013 Author Share Posted January 19, 2013 YOU ROCK! mysql_fetch_row() works perfectly. Also, please try to switch to PDO or mysqli. Those two are more efficient, can make your code much safer, and offer more functionality. Sorry to be so naive but how would I do that based on the code above? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406987 Share on other sites More sharing options...
rseigel Posted January 19, 2013 Author Share Posted January 19, 2013 To add to this when I change it to $result = mysqli_query It works without error but the csv file is corrupted and won't open in excel or calc. Quote Link to comment https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406991 Share on other sites More sharing options...
Barand Posted January 20, 2013 Share Posted January 20, 2013 You cannot just substitute mysqli for mysql in the function names, the function parameters are different. Personally I prefer object mysqli eg $mysqli = new mysqli ('host', 'user', 'pwd', 'database'); $result = $mysqli->query("SELECT .... "); while ($row = $result->fetch_row()) { // process row } Quote Link to comment https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406994 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2013 Share Posted January 20, 2013 Using mysqli is more than just changing the _query statement. The other database statements must be changed as well to match. Posting your current code would help, and if you are not getting any php detected errors, either you don't have php's error_reporting/display_errors set up or the rest of the code on your page is doing something, like redirecting w/output buffering on, so that the errors are being hidden. Quote Link to comment https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406995 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.