【发布时间】:2013-05-10 10:18:31
【问题描述】:
我有以下代码。有没有什么方法可以合并简化?
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_* 已被弃用。看来您不使用 $current 变量,为什么要获取文件的内容?你想用 foreach 做什么,我不明白
标签: php loops for-loop phpmyadmin while-loop