0

Hi I'm new to PHP and I'm trying to get data from MySQL table as array of JSON. I did get the array of JSON from PHP file, but there is an empty array on the first index.

Here is my table structure from creating on MySQL it :

CREATE TABLE IF NOT EXISTS `tbl_employee` (
  `employee_id` int(4) NOT NULL AUTO_INCREMENT,
  `employee_name` varchar(60) NOT NULL,
  `designation` varchar(30) NOT NULL,
  `hired_date` date NOT NULL,
  `salary` int(10) NOT NULL,
  PRIMARY KEY (`employee_id`),
);

INSERT INTO `tbl_employee` (`employee_id`, `employee_name`, `designation`, `hired_date`, `salary`) VALUES
(1, 'Steve', 'VP', '2013-08-01', 60000),
(2, 'Robert', 'Executive' '2014-10-09', 20000),
(3, 'Luci', 'Manager', '2013-08-20', 40000);
(4, 'Joe', 'Executive', '2013-06-01', 25000);
(5, 'Julia', 'Trainee', '2014-10-01', 10000);

Here is my PHP file:

<?php
    //open connection to mysql db
    $dbhost = 'clvm.ddns.net:8026';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'ta_trial';
    //$conn = mysql_connect($dbhost, $dbuser, $dbpass);

    $connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("Error " . mysqli_error($connection));

    //fetch table rows from mysql db
    $sql = "select * from tbl_employee";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $emparray[] = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }

    echo json_encode($emparray);
?>

And this is what I got:

[[],{"employee_id":"1","employee_name":"Steve","designation":"VP","hired_date":"2013-08-01","salary":"60000"},{"employee_id":"2","employee_name":"Robert","designation":"Executive","hired_date":"2014-10-29","salary":"20000"},{"employee_id":"3","employee_name":"Luci","designation":"Manager","hired_date":"2013-08-20","salary":"40000"},{"employee_id":"4","employee_name":"Joe","designation":"Executive","hired_date":"2013-06-01","salary":"25000"},{"employee_id":"5","employee_name":"Julia","designation":"Trainee","hired_date":"2014-10-01","salary":"10000"}]

I don't understand why would there be an empty array on the first index? Can anyone point the mistake or anything I did wrong here? Is there any better solution to do this? Thank you, any help would really be appreciated.

1 Answer 1

2

you miss something $emparray[] = array(); to $emparray = array();

<?php
    //open connection to mysql db
    $dbhost = 'clvm.ddns.net:8026';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'ta_trial';
    //$conn = mysql_connect($dbhost, $dbuser, $dbpass);

    $connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname) or die("Error " . mysqli_error($connection));

    //fetch table rows from mysql db
    $sql = "select * from tbl_employee";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $emparray = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }

    echo json_encode($emparray);
?>

check the result ok

Sign up to request clarification or add additional context in comments.

2 Comments

ah so sorry my bad, thank you very much for pointing it out! it works!
its fine, sometimes we read our code from top to bottom finding what miss.. but its just annoying character..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.