dtnbjjq51949 2019-07-15 17:31 采纳率: 0%
浏览 112
已采纳

如何使用PHP查询视图并将编辑后的数据写入outfile

Goal: Use php to read, parse, & edit and write data from a view into a pipe delimited outfile.

Setup: I have a view “my_view” that has 11 columns/fields. I need data from the first 10 written to the pipe delimited file.

Complicating Factor: The tenth field has a string of text that may contain many comma separated values or it may be blank

Requirement: Output a file that has data separated by exactly 26 pipes

My conceptual solution: Use php to query the view and return the table with fields separated by commas, then find and replace all commas with pipes. Then calculate to ensure that each record/row has a total of 26 pipes even if there is no data to separate.

[I am using nuBuilder and can attach php to a button to initiate an action. I will attach this code to be executed by the button in nubuilder. I am able how to insert the code in nubuilder when I figure out how to write the php]

I've attempted solving this as a stored procedure in phpMyadmin but I can't seem to dynamically calculate out the required number of pipes.

My view (which I want the data from) looks like this:

Select
  Signals.SignalName
  Signals.SddName
  Signals.Address
  Signals.Word
  Signals.MSB
  Signals.LSB
  Signals.Resolution
  Signals.DataType
  Signals.Units
  Enumerations.TypeName (This is the column containing string of data        seperated by colums.
  Signals.FK_EnuID
  WHERE Signals FK_EnuID = Enumeratiopns.ID

I think I've come up with the following code:

<?php
$servername='localhost';
$username = 'user';
$password = 'password';
$dbname = 'database';

//Create a connection
$con = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM my_view";
$result = $conn->query($sql);

if (result->num_rows > 0) {
 while ($row = $result->fetch_assoc()){

//string replace commas with pipes
$data = str_replace (',', '|', $row);

//Count number of pipes
$fields = substr_count($row, '|');

//Calculate the number of empty fields & insert pipes
$emptyfields = str_repeat('$|', (26-$fields));

//concatenate data with empty fields
$data .= $emptyfields;

//write data to file
file_put_contents (signals.dat, $data);
 }//end while
}
else{
      echo "0 results";
}
?>
  • 写回答

2条回答 默认 最新

  • douyou1857 2019-07-23 18:14
    关注
    <?php
    $servername = 'myserver';
    $username = 'user';
    $password = 'password';
    $dbname = 'mydb';
    
    //create a connection
    $con = new mysqli($servername, $username, $password, $dbname, 3306);
    $sql = "SELECT * FROM my_view";
    $result = $con->query($sql);
    $rowcount = 0;
    $filecount = 1;
    
    if ($result->num_rows > 0) {
      while ($row = $result->fetch_assoc()){
    //build logic for splitting file.
    $rowcount = $rowcount +1;
    
    if (($rowcount % 10000)==)){
      $filecount = $filecount +1;
    
    //join row elements into a string
    $rowstr = join(","$row);
    
    //String Replace commas with Pipes
    $rowstr2 = str_replace(',', '|', $rowstr);
    
    //Count number of Pipes
    $fields = substr_count($rowstr2, '|');
    
    //Calculate the number of empty fields & insert pipes
    $emptyfields = str_repeat('|', 26-$fields));
    
    //Concatenate data with empty fields
    $rowstr2 .= $emptyfields;
    $rowstr2 .= PHP_EOL;
    $filename = 'signals_'.$filecount.'.dat';
    
    //Write data to file
    file_put_contents ($filename, $rowstr2, FILE_APPEND);
     }//end while
    }
    else {
      echo "0 results";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘