-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"}
4
  • 1
    possible duplicate of mysql_fetch_array and only string array keys Commented Dec 30, 2013 at 5:51
  • Where does u assign the value to $array Commented Dec 30, 2013 at 5:53
  • Your code is prone to SQL injection; please look at the linked alternatives for making MySQL queries. Commented Dec 30, 2013 at 5:54
  • Now that i have the json view correctly, it seems that the json is returning different objects separated. please see update json above. Commented Dec 30, 2013 at 5:56

1 Answer 1

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

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

Comments

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.