尝试从 PHP 中的 MySQL 数据库生成 JSON 数组时出错
Error while trying to generate JSON Array from MySQL Database in PHP
这是我的 PHP get_categories.php 代码:
<?PHP
require_once('connection.php');
$query="SELECT * FROM categories";
$result = mysqli_query($connection,$query);
$return_arr = array();
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
{
$row_array['category'] = $row['category'];
$row_array['icon'] = $row['icon'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
?>
和connection.php
<?php
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = "password"; //replace it with your database password
$dbname = "db_client";
// Create connection
$connection = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
?>
当我运行get_categories.php生成一个json数组..出现这个错误
解析错误:语法错误,意外的“;”在 C:\xampp\htdocs\get_categories.php 第 7 行
有人可以纠正我做错了什么吗?谢谢。
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
应该是:
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc))
你少了一个括号
这是我的 PHP get_categories.php 代码:
<?PHP
require_once('connection.php');
$query="SELECT * FROM categories";
$result = mysqli_query($connection,$query);
$return_arr = array();
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
{
$row_array['category'] = $row['category'];
$row_array['icon'] = $row['icon'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
?>
和connection.php
<?php
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = "password"; //replace it with your database password
$dbname = "db_client";
// Create connection
$connection = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
?>
当我运行get_categories.php生成一个json数组..出现这个错误
解析错误:语法错误,意外的“;”在 C:\xampp\htdocs\get_categories.php 第 7 行
有人可以纠正我做错了什么吗?谢谢。
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
应该是:
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc))
你少了一个括号