0

我正在尝试从

  • 表:艺术家
  • 行:艺术家

成这种格式。艺术家姓名可以有特殊字符,其中有超过 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!");
4

1 回答 1

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 将加载这个文件

于 2012-11-11T01:42:13.683 回答