我从文本文件结构中读取如下: AAA BBB CCC 并在数组中写入,现在我需要使用此名称进行查询,选择所有具有此名称的colounms并将结果写入csv文件,文件读取工作正常但查询只返回1行,只返回ccc最后一个数组。 帮助我。
这是我的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "tirocinioriginale";
$con = mysqli_connect($servername, $username, $password,$db);
if (!$con) {
die("Connessione fallita: " . mysqli_connect_error());
}
$myfile = fopen("aziende1.txt", "r") or die("Unable to open file!");
//echo fread($myfile,filesize("aziende1.txt"));
$aziende = array();
while (!feof($myfile)) {
$aziende[] = fgets($myfile);
}
fclose($myfile);
echo implode(',',$aziende);
$query = "SELECT * FROM azienda WHERE nome IN ('".implode("','",$aziende)."')";
$result = $con->query($query);
$azienderow = array();
while($row = $result->fetch_assoc())
{
array_push($azienderow, $row);
}
echo "Connessione successo";
var_dump($azienderow);
//$row = mysql_fetch_assoc($result);
/* $fp = fopen('dumpaziende.csv', 'w');
foreach ($row as $val) {
fputcsv($fp, explode($val));
}
fclose($fp);
*/
//var_dump($aziende);
mysqli_close($con);
?>
如果使用我的代码,结果是: 文件: aitech ItalData
aitech,ItaldataConnessione successoarray(1){[0] =&gt; array(12){[“id_azienda”] =&gt; string(3)“244”[“nome”] =&gt; string(8)“ItalData”[“settore”] =&gt; string(17)“Sviluppo software”[“website”] =&gt; string(5)“sadsa”[“email”] =&gt; string(11)“dasdasddsad”[“curriculum_aziendale”] =&gt; string(9)“dasdasdas”[“tematiche”] =&gt; string(7)“fafdaew”[“n_borse”] =&gt; NULL [“data_stipula_convenzione”] =&gt; string(10)“2018-03-14”[“data_fine_convenzione”] =&gt; string(10)“2018-03-20”[“attiva”] =&gt; string(1)“1”[“tipologia_id”] =&gt; string(1)“3”}}
答案 0 :(得分:0)
echo implode(',',$aziende);
产地:
aitech ,Italdata
// ^-- see this space? this is probably the problem
查询将呈现如下:
$query = "SELECT * FROM azienda WHERE nome IN ('aitech ','Italdata')";
// the trailing space will impact matching --------^
您可以使用:
implode("','",array_map('trim',$aziende))
或trim()
在while
循环中进行修剪,然后再存储数据。