Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/273367-silly-array-issue/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406985
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406987
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406994
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/273367-silly-array-issue/#findComment-1406995
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.