【发布时间】:2017-08-10 10:25:10
【问题描述】:
我试图只获取一个表名,以便包含表名以创建一组字典项。这是我的桌子目前的样子:
[
{
id: "1",
track: "Revolution",
artist: "Lou Yoellin",
file: "Revolution.mp3"
},
{
id: "2",
track: "Superstitious",
artist: "Random Artist",
file: "Superstitious.mp3"
}
]
我想更改为在数组之前添加我的表的名称、歌曲:
songs: [
{
id: "1",
track: "Revolution",
artist: "Lou Yoellin",
file: "Revolution.mp3"
},
{
id: "2",
track: "Superstitious",
artist: "Random Artist",
file: "Superstitious.mp3"
}
]
我不想抢多张桌子,而只想抢一张。下面是我的 PHP 代码。我感觉我需要做的就是更改 SQL 命令,但我对编程和数据库检索还很陌生。
$con=mysqli_connect("x","x","x","x");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Songs'
$sql = "SELECT * FROM songs";
// Show all Tables
// $sql = "SHOW TABLES FROM myiosapp";
// $sql = "SELECT songs FROM myiosapp.tables";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
【问题讨论】: