0

私はphpでコードを書きました:

  1. txtファイルを開き、
  2. 一行ずつ読んで、
  3. 関数を使用しexplode()て行を分割します (スペースに基づく)
  4. 各部分文字列を変数に割り当て、
  5. すべてをデータベースに挿入します。

これらの行には、数値と場合によってはNULL値があります。問題は、すべてを mysql に挿入すると、null 値が0ではなく として表示されることですnull

txt からの null 値を ..db のテーブルに null 値として表示することは可能ですか?

何か明確に説明していない場合はお知らせください。

ありがとうございました。
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); 

   ?>

CREATE TABLE クエリは次のとおりです。

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
4

1 に答える 1

1

NULL を MySQL に渡すには、まさにそれを行います。

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

コードで、$var、$var2 が null かどうかを確認し、null の場合は、'$var' または '$var2' の代わりに NULL を使用します。

$var = !empty($var) ? "'$var'" : "NULL";
$var2 = !empty($var2) ? "'$var2'" : "NULL";

$query = "INSERT INTO data (field1, field2)
      VALUES ('$var', '$var2',)";
于 2012-11-04T17:37:35.113 に答える