我正在尝试从
转换成这种格式。艺术家的名字可以有特殊的人物,其中有超过16000。它需要写到文件中。例如,调用任何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!");发布于 2012-11-11 01:42:13
是的,尝试使用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将加载该文件。
https://stackoverflow.com/questions/13325641
复制相似问题