I'm trying to get a json array of this format :
var js_array = [
["Name1", "Address1", "Url1"],
["Name2", "Address2", "Url2"],
etc...
]
from a MySQL query. Here is my database :
+---------+-------------+---------+
| Name | Address | URL |
+---------+-------------+---------+
| Name1 | Address1 | Url1 |
| Name2 | Address2 | Url2 |
| Name3 | Address3 | Url3 |
+---------+-------------+---------+
I tried this :
$query = $bdd->query('SELECT Name, Adress, URL FROM festivals');
while ($row = $query->fetch()) {
$array[] = $row['Name'];
$array[] = $row['Adress'];
$array[] = $row['URL'];
}
//Then I return it as an js array that I use later
$js_array = json_encode($array);
The result is :
["Name1", "Address1", "Url1", "Name2", "Address2", "Url2", "Name3", "Address3", "Url3"]
I also tried this :
$query = $bdd->query('SELECT Name, Adress, URL FROM festivals');
while ($row = $query->fetch()) {
$array[] = $row;
}
//Then I return it as an js array that I use later
$js_array = json_encode($array);
Which returns this :
[
{0: "Name1", 2: "Address1", 2: "Url1", Name: "Name1", Address: "Address1", URL: "Url1"},
{0: "Name2", 2: "Address2", 2: "Url2", Name: "Name2", Address: "Address2", URL: "Url2"}
]
Is there a way to get what I'd like ? Thanks
You just need to create an array for each row which has only the values in it:
$query = $bdd->query('SELECT Name, Adress, URL FROM festivals');
while ($row = $query->fetch()) {
$array[] = array($row['Name'], $row['Adress'], $row['URL']);
}
//Then I return it as an js array that I use later
$js_array = json_encode($array);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.