-1

I am running the following code to query two tables, the json loaded with duplicate values with different strings. could you please help me tackle this issue.

Code.php

    $acctname = $_REQUEST['acctname'];

      $items = mysql_query("SELECT * FROM account_items WHERE acctname = '$acctname'")or die(mysql_error());

        while ($rows = mysql_fetch_array($items)) {
         $itemType = $rows['item_type'];
         $itemid = $rows['id'];


      $Qitems_cat = mysql_query("SELECT * FROM `$itemType` WHERE ".$itemType.'_'."id = '$itemid' ")or die(mysql_error());
          $array = array();
          while ($result = mysql_fetch_array($Qitems_cat)) {
            echo json_encode($array);                                                                 
         }    
      }

Result.json note the "0" is the same as "network_id" and so on.

{"network_id":"16","acctname":"Test","networkname":"networkOne","networkIP":null}{"Server_id":"2","acctname":"Test","servername":"test server"}
BEBO
  • 47
  • 1
  • 3
  • 10
  • 1
    possible duplicate of [mysql\_fetch\_array and only string array keys](http://stackoverflow.com/questions/7440479/mysql-fetch-array-and-only-string-array-keys) – Ignacio Vazquez-Abrams Dec 30 '13 at 05:51
  • Where does u assign the value to `$array` – Nouphal.M Dec 30 '13 at 05:53
  • Your code is prone to [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php); please look at the linked alternatives for making MySQL queries. – Kevin Ji Dec 30 '13 at 05:54
  • Now that i have the json view correctly, it seems that the json is returning different objects separated. please see update json above. – BEBO Dec 30 '13 at 05:56

1 Answers1

2

You can use mysql_fetch_assoc() instead of mysql_fetch_array()

Ref: http://php.net/mysql_fetch_assoc

Note: mysql extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used

Krish R
  • 22,583
  • 7
  • 50
  • 59