我有以下代碼。有什麼方法可以結合並簡化它嗎?For和While循環在php mysql中
json.html文件中的輸出應如下所示:["abc","def","ghi"]
。
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "admin") or die(mysql_error());
mysql_select_db("test1") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM test_auto_complete") or die(mysql_error());
$menu = array();
while($row = mysql_fetch_assoc($result))
{
$menu[] = array("id" => $row['username'],);
}
foreach($menu as $key=>$value)
{
$menu[$key] = $value['id'];
}
$my_json_content = json_encode($menu);
$file = 'json.html';
$current = file_get_contents($file);
file_put_contents($file, $my_json_content);
?>
我知道代碼看起來很糟糕,但即便如此,有人可以幫助我嗎?
謝謝
阿里漢
請勿使用mysql_ *將它們更改爲PDO或mysqli原因mysql_ *是depracated。看來你不使用$ current變量,所以你爲什麼得到文件的內容?你想使用foreach,我沒有得到它 – Robert 2013-05-10 10:20:16