我正在尝试从
输出数据采用这种格式。 艺术家姓名可以有特殊字符,其中超过16k。 它需要写入文件。叫做artist.php的任何东西,例如
$Artist = array(
"Name from database",
"Name from database",
"Name from database",
"Name from database",
"Name from database"
);
很抱歉没有解释。为ajax自动完成这样做..所以我需要在其中创建一个包含此数组的文件。
我有它工作并输出我需要它的方式只是坚持在完成后如何对文件说这一切。
这是我所拥有的
require_once('config.php');
$res = mysql_query("SELECT artist FROM artists");
$ids = array();
$NotAllowed = array("(", ")", "+", ".", "-", "$", "#", "'", "\"", "%",":", ",", "*", "\\", "/");
echo '$aUsers = array('; echo "<br>";
while ($row = mysql_fetch_assoc($res)) {
$ids[] = $row['artist'];
$FixedArtist = str_replace($NotAllowed, " ", $row['artist']);
echo "\""; echo $FixedArtist; echo "\","; echo "<br>";
}
echo "\"Lastname\""; echo "<br>";
echo ");";
$FileToSaveTo = "ajax-search.php";
$fp = fopen($FileToSaveTo,"w") or die ("Error opening file in write mode!");
fputs($fp,$content);
fclose($fp) or die ("Error closing file!");
答案 0 :(得分:0)
是尝试使用PDO读取结果并将其导出到单独的artists.json文件中,它更干净
/*require_once('config.php');
$res = mysql_query("SELECT artist FROM artists");
$ids = array();
$NotAllowed = array("(", ")", "+", ".", "-", "$", "#", "'", "\"", "%",":", ",", "*", "\\", "/");
/*while ($row = mysql_fetch_assoc($res)) {
$ids[] = str_replace($NotAllowed, " ", $row['artist']);
}*/
//PDO version
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sth = $dbh->prepare("SELECT artist FROM artists");
$sth->execute();
$ids = $sth->fetch(PDO::FETCH_ASSOC);
//replace not allowed chars here
file_put_contents('artists.json', json_encode($ids));
然后ajax-search.php将加载此文件