Error while trying to generate JSON Array from MySQL Database in PHP

82 views Asked by At

Here is my PHP code for 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);

    ?>

and 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());
}
?>

When i run the get_categories.php to generate a json array.. this error appears

Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\get_categories.php on line 7

Can someone correct me on what i am doing wrong? Thanks.

1

There are 1 answers

2
Jaime On BEST ANSWER

while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc)
should be :
while ($row = mysqli_fetch_array($result, mysqli_fetch_assoc))

You're missing a parenthesis