目标:使用php读取,解析和编辑视图中的数据,并将其写入管道定界的输出文件中。
设置:我有一个视图“ my_view”,其中包含11列/字段。我需要将前10个数据写入管道分隔文件。
复杂因素:第十字段包含一串文本,其中可能包含许多逗号分隔的值,也可以为空白
要求:输出一个文件,该文件的数据正好由26个管道分隔
我的概念性解决方案::使用php查询视图并返回表,并用逗号分隔字段,然后用管道查找并替换所有逗号。然后计算以确保即使没有数据要分离,每个记录/行也总共有26个管道。
[我正在使用nuBuilder,可以将php附加到按钮上以启动操作。我将附加此代码,以通过nubuilder中的按钮执行。当我弄清楚如何编写php时,我能够在nubuilder中插入代码。
我试图解决这作为phpMyadmin中的存储过程,但我似乎无法动态地计算出所需的管道数量。
我的视图(我想从中获取数据)如下:
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
我想我已经提出了以下代码:
<?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";
}
?>
答案 0 :(得分:1)
为什么有任何PHP代码?
给出此查询:
SELECT province, city, population
INTO OUTFILE '/tmp/xx.txt' COLUMNS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
FROM canada
WHERE population > 0
LIMIT 11;
它将生成文件xx.txt
:
"Alberta"|"Killam"|1056
"Alberta"|"Smoky Lake"|1063
"Alberta"|"Irricana"|1091
"Alberta"|"Viking"|1106
"Alberta"|"Two Hills"|1147
"Alberta"|"Spirit River"|1157
"Alberta"|"Bassano"|1388
"Alberta"|"Clairmont"|1483
"Alberta"|"Elk Point"|1514
"Alberta"|"Wembley"|1574
"Alberta"|"Bon Accord"|1611
同时,在您的代码中,if (...num_rows>0)
是多余的,因为如果没有行,while(...)
将不执行任何操作。删除if
将会修复该错误。该错误是result
而不是$result
。
答案 1 :(得分:0)
<?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";
}
?>