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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:19:10+00:00 2026-05-24T13:19:10+00:00

I’m trying to load data from a .txt file with PHP, but I fail

  • 0

I’m trying to load data from a .txt file with PHP, but I fail every time.
I tried to convert the text file to a CSV, but even there I fail.
My text file holds the content like this:

0001

120PCS LED MOVING HEAD LIGHT

TECHNICAL PARAMETER
Voltage: AC90V-120V or 200V-240V 50-60HZ
Power consumption:400W
Light source:120PCS 1W or 3W LED 
(R:30pcs?G:30pcs,B:30psc,W:30pcs)
Control mode:12HS
Operation mode: master-slave, auto movement,
Sound control: DMX 512
Each led source has an expectancy over 50000 to 100000 hours in theory 
Optical len angle:15 degrees
Level scanning:540 degrees Vertical scanning
270 degrees, speed adjustable 
Indefinite RGBW color mixing system
LCD display adopted
Product size:512*402*555mm
N.W:19kg  G.W:21kg

0002

LED 108PCS MOVING HEAD LIGHT

TECHNICAL PARAMETER
Voltage:AC100V-240V ,50/60HZ 
Power consumption:306W
Light source:108pcs of 1/3W LED 
Operation mode master-slave, sound control,
auto movement,DMX512
Control channel:11Hs
Level scanning angle:540 degrees 
Vertical scanning angle:270degrees 
Quick electronic dimmer, strobe from 1 to 20 times/second
Smooth RGB mixing system &
Rainbow effect(can add white)
Beam angle:15 degrees
Package size :420*330*550mm
N.W:10kg  G.W:13kg

I tried to use:

 <?php
 $handle = @fopen("data.txt", "r");
 $conn = mysql_connect("localhost","root",""); 
 mysql_select_db("mytext",$conn);
 while (!feof($handle)) // Loop til end of file.
 {
 $buffer = fgets($handle, 4096);
  // Read a line.
 list($a,$b,$c)=explode("|",$buffer);
 //Separate string by the means of |
 echo $a."-".$b."-".$c."<br>";
 $sql = "INSERT INTO data_table (id, name, age) VALUES('".$a."','".$b."','".$c."')";   
 mysql_query($sql,$conn) or die(mysql_error());
 }
 ?>

But I can’t figure out how to insert it nicely.

value a = 0001
value b = 120PCS LED MOVING HEAD LIGHT
value c = Technicalparameter etc etc etc

value a = 0002
value b = LED 108PCS MOVING HEAD LIGHT
value c = Technicalparameter etc etc etc

I have trouble with delimiters.

  • 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-05-24T13:19:10+00:00Added an answer on May 24, 2026 at 1:19 pm

    The following code will work, assuming your input file is as shown above. That is:

    • Contains 3 fields per record
    • Fields in file separated by blank line
    • Number of fields in file evenly divisible by 3.

          <?php
          /* For simplicity in this example, I will assume the db table
           * conforms to the following schema:
           * 
           * CREATE TABLE products(
           *   id VARCHAR(5),
           *   item VARCHAR(100),
           *   item_details TEXT
           * );
           */
      
          /* Make the db connection */
          $link = mysql_connect('localhost', 'mysql_user', 'user_pwd');
          if (!$link) {
              die('Could not connect to db.');
          }
      
          // set the db.
          $db_selected = mysql_select_db('test', $link);
          if (!$db_selected) {
              die ("Can't access the db");
          }
      
          /* Read entire file into string. */
          $fileContents = file_get_contents('f1.txt');
      
          /* Set line ending character based on particular OS. 
           * Unix       = \n
           * Macintosh  = \r
           * MS Windows = \r\n
           */
          $lec = "\r\n"; // My system is MS Windows.
      
          /*Fields are separated by a blank line so the delimiter between
           * fields is double line ending character ($lec).
           */
          $delimiter = "$lec$lec";
      
          /* Build array of field data for all records. */
          $contentArray = explode($delimiter, $fileContents); 
      
          /* Get the size of the array. */
          $numElements = count($contentArray);
      
          /* Because we have 3 fields per record, $numElements should be 
           * evenly divisible by 3.
           */
          $numElelemtsPerRec = 3;
          if($numElements % $numElelemtsPerRec > 0) 
            exit("File parse error. Contents of file does not fill records.");
      
          /* Now build SQL input records. 
           * One record contails 3 ($numElelemtsPerRec) array elements.
           */
          for ($i = 0; $i <= $numElements - 1; $i = $i + $numElelemtsPerRec) {
              $id = mysql_real_escape_string($contentArray[$i]);
              $item = mysql_real_escape_string($contentArray[$i+1]);
              $item_details = mysql_real_escape_string($contentArray[$i+2]);
              $records .= "('$id', '$item', '$item_details'), ";
              echo("id = $id<br>
                    item = $item<br>
                    item_details = $item_details<br>
                    <hr>");
          }
      
          /* Now trim the ending comma and space from string of records. */
          $records = substr($records, 0, -2);
          //exit($records);
      
          /* Build and execute the SQL INSERT query. */
          $query = "INSERT INTO products (id, item, item_details) 
                   VALUES $records";
          $result = mysql_query($query);
      
          /* Check for db error. */
          if(!$result) echo("Query Error! Process aborted.");
      ?>
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.