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 8253377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:51:27+00:00 2026-06-08T00:51:27+00:00

There is a mysql database named: example which has a table named: products it

  • 0

There is a mysql database named: example which has a table named: products it have fields: products_model and products_image. the model field is unique.

now i have a csv file named data.csv. which have two column products_model and products_image.i use the following code. but it doesn’t import the data into the database.what’s wrong with my code?

    $fp = fopen("data.csv","r");
$length = 4096; //have to be optional length
$fildDelineate = ',' ;//or "|" declare what you need
$dbhost="localhost";
$dbname="zencartone";
$dbuser="root";
$dbpass="";
$link  = mysql_connect($dbhost,$dbuser,$dbpass);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname);

$databasetable  = 'products';

$counter = 1;//to omission first row if it is table headers

while($line = fgetcsv($fp,$length,$fildDelineate)){
    foreach($line as $key=>$value){
        //for example insert in 3 column
        $importSQL = "insert into $databasetable  (products_model, products_image) values('$line[0]','$line[1]')";
        mysql_query($importSQL) or die(mysql_error());
    }
    $counter++;
}
fclose($fp);

data.csv:

have two column:

products_model  products_image
MG200MMS    dress/201247561673.jpg
MG400-32MB  dress/2012471141673.jpg
MSIMPRO dress/201247741673.jpg
DVD-RPMK    dress/2012471831673.jpg
DVD-BLDRNDC dress/2012474221673.jpg
DVD-MATR    dress/201112132056773.jpg
  • 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-08T00:51:28+00:00Added an answer on June 8, 2026 at 12:51 am

    Edit:

    Use the following loop:

    while($line = fgets($fp)) {
        $lineArray = explode("\t", $line); // Use tab as delimiter.
        $importSQL = "insert into $databasetable  (products_model, products_image) values ('".$lineArray[0]."','".$lineArray[1]."')";
        mysql_query($importSQL) or die(mysql_error());
        $counter++;
    }
    

    SQL errors:

    $importSQL = "insert into $databasetable values('$line[0]','$line[1]')";
    

    You need to specify the column names:

    $importSQL = "insert into $databasetable (col1, col2) values('$line[0]','$line[1]')";
    

    Edit: fgetcsv($fp,$length,$fildDelineate,'')

    Enclosure param is set to empty string. You can get rid of it if you have no enclosures around the values in the CSV:

    fgetcsv($fp,$length,$fildDelineate)
    

    Otherwise, set the correct enclosure (double quotes or single quote).

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

Sidebar

Related Questions

I have a mysql database in which there is about hundreds of category for
Suppose I have a MySQL database named DB, and a table named ContactInfo. The
The table names in my mysql database are dynamically generated. Is there some way
Is there any application that will read a MySQL database table and generate a
I have a MySQL database called csftdb and in that database there exists 1
In a Wordpress MySQL database, in wp_postmeta table , there are most rows with
I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have a column in a MySQL database which is currently all different lengths
I've got my table called Device_tbl which has the following columns: Image Manufacturer Model
I have a textbox which i echo data from mysql. However if there is

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.