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

  • Home
  • SEARCH
  • 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 8781301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:15:57+00:00 2026-06-13T20:15:57+00:00

I have written a code in php where I: Open a txt file, Read

  • 0

I have written a code in php where I:

  1. Open a txt file,
  2. Read each line,
  3. Use the explode() function to break the line into pieces (Based on spaces)
  4. Assign each substring to variables and
  5. Insert everything in the database.

On these lines I have numbers and sometimes NULL values. The problem is that when I insert everything to mysql the null values are presented as 0 instead of null.

Is it possible to display the null values from the txt as.. null values in my table in db?

Please let me know if I didn’t explain something clear.

Thank you.
d.

<!DOCTYPE HTML>
<?php

// This code:
// 1) opens a txt file, which is stored in a specified folder,
// 2) reads each line of the file,
// 3) inserts the data in the specified database


// Connect to the database
include('connect.php'); 
// Define the location of the txt file.
$myFile = "/Applications/MAMP/htdocs/php_test/data_correct.txt";
// Open the file
$fh = fopen($myFile,'r') or die ("Could not open:".mysql_error());
// !feof means "not the end of the file"
while (!feof($fh)){
// fgets: gets the first line of $fh and assigns it to the variable $line
// Each line can be up to 5000 characters long
$line = fgets($fh,5000);
 // explode function separated the string by means of "tab" space
  list($WN,$TOW,$Receiver_ID,$lat,$lon,$height,$SVID,$Azimuth,$Elevation,$Total_S4_L1,$SNR_L1,$Phi01_L1,$Phi03_L1,$Phi10_L1,$Phi30_L1,$Phi60_L1,$TEC_TOW_45,$dTEC_TOW_60_45,$TEC_TOW_30,$dTEC_TOW_45_30,$TEC_TOW_15,$dTEC_TOW_30_15,$TEC_TOW,$dTEC_TOW_15_TOW,$L1_lock_time,$L2_lock_time,$SI_index_L1,$Total_S4_L2,$SNR_L2,$Phi01_L2,$Phi03_L2,$Phi10_L2,$Phi30_L2,$Phi60_L2,$Lock_L2,$SI_index_L2,$Total_S4_L5,$SNR_L5,$Phi01_L5,$Phi03_L5,$Phi10_L5,$Phi30_L5,$Phi60_L5,$Lock_L5,$SI_index_L5)=explode(" ",$line);

  // insert data into table in database. The name of the table is GPS.
 // The primary key increases automatically. The null values are acceptable.

    $query = ("INSERT INTO GPS      (WN,TOW,Receiver_ID,lat,lon,height,SVID,Azimuth,Elevation,Total_S4_L1,SNR_L1,Phi01_L1,Phi03_L1,Phi10_L1,Phi30_L1,Phi60_L1,TEC_TOW_45,dTEC_TOW_60_45,TEC_TOW_30,dTEC_TOW_45_30,TEC_TOW_15,dTEC_TOW_30_15,TEC_TOW,dTEC_TOW_15_TOW,L1_lock_time,L2_lock_time,SI_index_L1,Total_S4_L2,SNR_L2,Phi01_L2,Phi03_L2,Phi10_L2,Phi30_L2,Phi60_L2,Lock_L2,SI_index_L2,Total_S4_L5,SNR_L5,Phi01_L5,Phi03_L5,Phi10_L5,Phi30_L5,Phi60_L5,Lock_L5,SI_index_L5)  
     VALUES   ('".$WN."','".$TOW."','".$Receiver_ID."','".$lat."','".$lon."','".$height."','".$SVID."','".$Azimuth."','".$Elevation."','".$Total_S4_L1."','".$SNR_L1."','".$Phi01_L1."','".$Phi03_L1."','".$Phi10_L1."','".$Phi30_L1."','".$Phi60_L1."','".$TEC_TOW_45."','".$dTEC_TOW_60_45."','".$TEC_TOW_30."','".$dTEC_TOW_45_30."','".$TEC_TOW_15."','".$dTEC_TOW_30_15."','".$TEC_TOW."','".$dTEC_TOW_15_TOW."','".$L1_lock_time."','".$L2_lock_time."','".$SI_index_L1."','".$Total_S4_L2."','".$SNR_L2."','".$Phi01_L2."','".$Phi03_L2."','".$Phi10_L2."','".$Phi30_L2."','".$Phi60_L2."','".$Lock_L2."','".$SI_index_L2."','".$Total_S4_L5."','".$SNR_L5."','".$Phi01_L5."','".$Phi03_L5."','".$Phi10_L5."','".$Phi30_L5."','".$Phi60_L5."','".$Lock_L5."','".$SI_index_L5."')");



    $result = mysqli_query($dbcon, $query) or die('no available data');

   }// end of while loop
  fclose($fh); 

   ?>

The CREATE TABLE QUERY IS:

CREATE TABLE `GPS` ( 
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `WN` INT,  # GPS Week Number
  `TOW` INT, # Time of Week (seconds)
  `Receiver_ID` VARCHAR( 30), # Name of GPS receiver
  `lat` FLOAT(10,6), # Latitude of the GPS receiver
  `lon` FLOAT(10,6), # Longitude of the GPS receiver
   `height` FLOAT(10,2), # Height
   `SVID` INT, #Satellite ID
   `Azimuth` INT, # Azimuth in degrees
   `Elevation` INT, #Elevation in degrees
   `Total_S4_L1` FLOAT(10,3), # Total S4 on L1 (dimensionless)
   `SNR_L1` FLOAT(10,1), # SNR on L1 (db)
   `Phi01_L1` FLOAT(10,3), # Phi01 on L1, 1-second phase sigma (radians)
   `Phi03_L1` FLOAT(10,3), # Phi03 on L1, 3-second phase sigma (radians)
   `Phi10_L1` FLOAT(10,3), # Phi10 on L1, 10-second phase sigma (radians)
   `Phi30_L1` FLOAT(10,3), # Phi30 on L1, 30-second phase sigma (radians)
   `Phi60_L1` FLOAT(10,3), # Phi60 on L1, 60-second phase sigma (radians)
   `TEC_TOW_45` FLOAT(10,4), # TEC at TOW -45s (TECU --> TEC SLUNT)
   `dTEC_TOW_60_45` FLOAT(10,4), # dTEC from TOW -60s to TOW -45s (TECU)
   `TEC_TOW_30` FLOAT(10,4), # TEC at TOW -30s (TECU --> TEC SLUNT)
  `dTEC_TOW_45_30` FLOAT(10,4), # dTEC from TOW -45s to TOW -30s (TECU)
   `TEC_TOW_15` FLOAT(10,4), # TEC at TOW -15s (TECU --> TEC SLUNT)
  `dTEC_TOW_30_15` FLOAT(10,4), # dTEC from TOW -30s to TOW -15s (TECU)
 `TEC_TOW` FLOAT(10,4), # TEC at TOW (TECU --> TEC SLUNT)
  `dTEC_TOW_15_TOW` FLOAT(10,4), # dTEC from TOW -15s to TOW (TECU)
  `L1_lock_time`  INT, # L1 lock time (seconds)
  `L2_lock_time` INT, #L2 lock time (seconds)
  `SI_index_L1` FLOAT (10,4), # SI Index on L1: (10*log10(Pmax)-  10*log10(Pmin))/(10*log10(Pmax)+10*log10(Pmin)) (dimensionless)
  `Total_S4_L2` FLOAT(10,3), # Total S4 on L2 (GPS/GLO) or E5a (GAL) (dimensionless)
   `SNR_L2` FLOAT(10,1), # SNR on L2 (dB)
  `Phi01_L2` FLOAT(10,3), # Phi01 on L2 (GPS/GLO) or E5a (GAL),  1-second phase sigma (radians)
   `Phi03_L2` FLOAT(10,3), # Phi03 on L2 (GPS/GLO) or E5a (GAL),  3-second phase sigma (radians)
   `Phi10_L2` FLOAT(10,3), # Phi10 on L2 (GPS/GLO) or E5a (GAL), 10-second phase sigma (radians)
   `Phi30_L2` FLOAT(10,3), # Phi30 on L2 (GPS/GLO) or E5a (GAL), 30-second phase sigma (radians)
   `Phi60_L2` FLOAT(10,3), # Phi60 on L2 (GPS/GLO) or E5a (GAL), 60-second phase sigma (radians)
  `Lock_L2`  INT, # Lock time of L2 (GPS/GLO) or E5a (GAL) (seconds)
  `SI_index_L2` FLOAT (10,4), # SI Index on L2 (GPS/GLO) or E5a (GAL) (dimensionless)
  `Total_S4_L5` FLOAT(10,3), # Total S4 on L5 (GPS/GLO) or E5a (GAL) (dimensionless)
 `SNR_L5` FLOAT(10,1), # SNR on L5 (dB)
 `Phi01_L5` FLOAT(10,3), # Phi01 on L5 (GPS/GLO) or E5a (GAL),  1-second phase sigma (radians)
  `Phi03_L5` FLOAT(10,3), # Phi03 on L5 (GPS/GLO) or E5a (GAL),  3-second phase sigma (radians)
   `Phi10_L5` FLOAT(10,3), # Phi10 on L5 (GPS/GLO) or E5a (GAL),  10-second phase sigma (radians)
  `Phi30_L5` FLOAT(10,3), # Phi30 on L5 (GPS/GLO) or E5a (GAL),  30-second phase sigma (radians)
  `Phi60_L5` FLOAT(10,3), # Phi60 on L5 (GPS/GLO) or E5a (GAL),  60-second phase sigma (radians)
  `Lock_L5`  INT, # Lock time of L5 (GPS/GLO) or E5a (GAL) (seconds)
  `SI_index_L5` FLOAT (10,4) # SI Index on L5 (GPS/GLO) or E5a (GAL) (dimensionless)
 ) # Default storage engine from version MySQL 5.5 is InnoDB
  • 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-13T20:15:58+00:00Added an answer on June 13, 2026 at 8:15 pm

    To pass a NULL to MySQL, you do just that.

    INSERT INTO table (field,field2) VALUES (NULL,'value')
    

    In the code, check if $var, $var2 are null, if they are, use NULL instead of ‘$var’ or ‘$var2’.

    $var = !empty($var) ? "'$var'" : "NULL";
    $var2 = !empty($var2) ? "'$var2'" : "NULL";
    
    $query = "INSERT INTO data (field1, field2)
          VALUES ('$var', '$var2',)";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written simple code to get content from xml file to php. $xml
I have written a PHP function to take a video embed code that has
I am learning file handling in php. I have written the following code to
I have written PHP code which connects to MYSQL to look up information, sends
I have written a code for taking JSON data from a PHP and putting
I have written a simple code in page test_cookie.php to work with cookies. if(isset($_GET['data']))
I have to written the following code to send a mail using PHP in
Our website code is written in PHP. We have very little testing in regards
I have written code that opens 16 figures at once. Currently, they all open
I have written code to perform a function that could take a while to

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.