Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:03:31+00:00 2026-06-06T17:03:31+00:00

I am attempting to create a csv file to update my inventory in Magento.

  • 0

I am attempting to create a csv file to update my inventory in Magento. My code is as follows:

    <?php
require_once ('../db.php');

$conn = db_connect();
$inventory = array();
$csvContent = "";
$n=0;


$result = $conn->query("select inventory.sku, book.author, book.title,
 book.publisher,     book.pub_date, book.edition,
inventory.isbn13, book.binding, book_condition.book_condition, defect.defect, note,    
feature, inventory.ourPrice, inventory.cost, inventory.quantity, subtitle, weight
from inventory
LEFT JOIN book on book.isbn13 = inventory.isbn13
LEFT JOIN defect on inventory.defect_id = defect.defect_id
LEFT JOIN note on inventory.note_id = note.note_id
LEFT JOIN feature on inventory.feature_id = feature.feature_id
LEFT JOIN book_condition on book_condition.condition_id = defect.condition_id
where inventory.quantity >0");

$num_rows = $result->num_rows;

if($num_rows > 0)
{ 
while($row = $result->fetch_assoc())
{
$inventory[$n] = array('sku' => $row['sku'],
        'author' => $row['author'],
                    /*'title' => $row['title'],
                    'publisher' => $row['publisher'],
                    'pub_date' => $row['pub_date'],
                    'edition' => $row['edition'],
                    'publisher' => $row['publisher'],
                    //'isbn10' => $isbn10,
                    'isbn13' => $row['isbn13'],
                    'binding' => $row['binding'],
                    'condition' => $row['condition'],
                    'defects' => $row['defect'],
                    'notes' => $row['note'],
                    'feature' => $row['feature'],
                    'price' => number_format($row['ourPrice'], 2, '.', ''),
        'cost' => $row['cost'],
                    'description' => $row['defect'],
                    'quantity' => $row['quantity'],
        'store' => "default",
        'websites' => "base",
        'attribute_set' => "books",
        'type' => "simple",
        'category' => "6",
        'type' => "simple",
        'image' => "/bcpics/".$row['isbn13'].".gif",
        'small_image' => "/bcpics/".$row['isbn13'].".gif",
        'thumbnail' => "/bcpics/".$row['isbn13'].".gif",
        'page_layout' => "No Layout updates",
        'options_container' => "Block after Info Column",
        'weight' => $row['weight'],
            'status' =>"Enables",
        'tax_class_id' =>"Taxable Goods", 
        'visibility'  =>"Catalog, Search",
        'enable_googlecheckout'  =>"yes",
        'is_recurring'  =>"no",
        'min_qty'  =>"0",*/
        'use_config_min_qty' =>"1",
        'is_qty_decimal'  =>"0",
        'backorders'  =>"0",
        'use_config_backorders' =>"1",
        'min_sale_qty' =>"1",
        'use_config_min_sale_qty' =>"1",
        'max_sale_qty'  =>"0",
        'use_config_max_sale_qty' =>"1",
        'is_in_stock' =>"1",
        'use_config_notify_stock_qty' =>"1",
        'manage_stock' =>"0",
        'use_config_manage_stock' =>"1",
        'stock_status_changed_automatically' =>"0",
        'use_config_qty_increments' =>"1",
        'qty_increments' =>"0",
        'use_config_enable_qty_increments' =>"1",
        'enable_qty_increments' =>"0",
        'store_id' =>"1",
        'product_type_id' =>"simple",
        'add_delete' => "",
        'url_key' => "",
        'gift_message_available' => "",
        'Topic' => "",
        'Subtitle'=> $row['subtitle'],
        'meta_title' => "",
        'meta_description' => "",
        'custom_design' => "",
        'url_path' => "",
        'special_price' => "",
        'meta_keyword' => "",
        'custom_layout_update' => "",
        'news_from_date' => "",
        'news_to_date' => "",
        'special_from_date' => "",
        'special_to_date' => "",
        'custom_design_from' => "",
        'custom_design_to' => "",
        'low_stock_date' => "",
        'notify_stock_qty' => "",
        'product_status_changed' => "",
        'product_changed_websites'=> "",
        'has_options'=> "0"

            );
    //print_r($inventory);die;
    $n++;
    } //end of while loop
} // end of if statement

$csvInventory = to_csv($inventory);

function to_csv( $array ) {
 $csv = "";

 if (count($array) == 0) return "No SKU's found";

 ## Grab the first element to build the header
 $arr = array_pop( $array );
 $temp = array();
 foreach( $arr as $key => $data ) {
   $temp[] = $key;
 }
 $csv = implode( ',', $temp ) . "\r\n";

 ## Add the data from the first element
 $csv .= to_csv_line( $arr );

 ## Add the data for the rest
 foreach( $array as $arr ) {
   $csv .= to_csv_line( $arr );
 }

 return $csv;
}

function to_csv_line( $array ) {
 $temp = array();
 foreach( $array as $elt ) {
   $temp[] = '"' . addslashes( $elt ) . '"';
 }

 $string = implode( ',', $temp ) . "\r\n";

 return $string;
}

$conn->close();

$myFile = "/home/bookcell/public_html/testbcos/web/inv/BCWebsite" . date("mdY") . ".csv";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $csvInventory;
fwrite($fh, $stringData);
fclose($fh);

My problem is that I can’t get it to work if I try to use all the parameters in the $inventory[$n] = array(). I can get all but about 30 rows in the array to work at one time, currently everything in the /* and */ will not work. I have played with changing the fields between /* and */ so I know that each row will go into the csv without a problem. I have checked the error logs on my server and there is no error, and the transfer log shows that it should be working.
Anyone see where I am going wrong here? Is there a better way to do this that will get the results I need?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:03:33+00:00Added an answer on June 6, 2026 at 5:03 pm

    I have exported 600MB+ sized csv files from PHP and MySQL in a similar way that you are with no issues. Sounds like a problem with your environment settings – not with the code or a limitation of the technologies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to add a function that will create a csv for download
Attempting to create an itemgroup for use in a target where the file types
So I'm attempting to create a Befunge interperter and reading a text file into
I am attempting to load data from a *.csv file off my VM of
What I am attempting to do is have an end CSV file like this:
I am attempting to stream a csv file as an attachment download. The CSV
I am attempting to create a php script that can connect thru ssh to
While attempting to create a replacement tablespace for USER, I accidentally created a datafile
Im attempting to create a new operator :? on lists, which operates the same
I attempting to create an 800x500 div with four overlapping divs inside the container

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.