-2
<?php
$username = "trainerapp";
$password = "password";
$hostname = "localhost";
$link     = @mysql_connect($hostname, $username, $password);

if (@mysql_select_db("trainer_registration")) {
    $select_query_num = @mysql_query("select id from program_details");
    $num_rows         = @mysql_num_rows($select_query_num);
    while ($row = @mysql_fetch_assoc($select_query_num)) {
        $j            = $row['id'];
        $select_query = @mysql_query("select id,program_name,company,date_prog from program_details where id = $j");
        $fetch_query  = @mysql_fetch_assoc($select_query);
        $id           = $fetch_query['id'];
        $pgmname      = $fetch_query['program_name'];
        $comp         = $fetch_query['company'];
        $datephp      = $fetch_query['date_prog'];

        $arr[] = array(
            $id,
            $pgmname,
            $comp,
            $datephp
        );

    }

    echo json_encode($arr);
}
?>

CURRENT OUTPUT:

[["1","Sample","Apple","2015-05-27"],["2","Sample 2","Lenovo","2015-05-28"],["14","3","3","09-29-2015"]]

I don't understand this output.

Questions:

  1. What does json_encode actually return? A string of all values or an array separated by ','? Because when I tried str.length() in js file, it returned 126

  2. How do I get output like single rows

    ["1","Sample","Apple","2015-05-27"]
    ["2","Sample 2","Lenovo","2015-05-28"]
    ["14","3","3","09-29-2015"]
    

in an array in javascript because I need to insert them in html under separate columns. That is, 1,2,14 under the column "ID", sample, sample 2, 3 under the column "Program name", and so on. Please help on parsing this array.

Vignesh Anandakumar
  • 167
  • 1
  • 3
  • 12
  • Where is old code always dredge up from? : – user2864740 May 10 '15 at 03:39
  • 1
    Perhaps you should read about JSON first?. BTW this snippet of PHP is awful. Don't use the @ operator, and don't use `MySQL_*()` –  May 10 '15 at 03:48
  • 1
    it sounds like you need to do a `json.parse()` on your returned data in javascript in order to turn your `json_encode()` string into a js object. – Sean May 10 '15 at 03:51

1 Answers1

-1

1 - http://php.net/manual/en/function.json-encode.php all is detailled there.

2 - Convert php array to Javascript it will help you.

3 - If you need an array, why do you use json_encode in your code.

4 - Do you know why do you use @ in many places? It's really bad to code like this.

Community
  • 1
  • 1
zeflex
  • 1,487
  • 1
  • 14
  • 29