-1

I have to produce Json object without any numeric sequence, How we will do that? I have to insert this Json info into table, which will be use in this question please find here

My json format

{
    "0": {
        "0": "65",
        "1": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "2": "JSS Layout, Mysore, Karnataka, India",
        "3": "Bangalore",
        "4": "ds",
        "5": "you@gmail.com",
        "6": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "7": "13-Feb-2015",
        "8": "10:30 AM",
        "9": "2",
        "10": "1220000000",
        "11": "Gajendra",
        "cId": "65",
        "address1": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "address2": "JSS Layout, Mysore, Karnataka, India",
        "city": "Bangalore",
        "comments": "ds",
        "email": "you@gmail.com",
        "landMark": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "scheduledDate": "13-Feb-2015",
        "scheduledTime": "10:30 AM",
        "services": "2",
        "userContactNumber": "1220000000",
        "userName": "Gajendra"
    }
}

I have remove numeric sequence from above and change like following

{
    "0": {
        "cId": "65",
        "address1": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "address2": "JSS Layout, Mysore, Karnataka, India",
        "city": "Bangalore",
        "comments": "ds",
        "email": "you@gmail.com",
        "landMark": "PWD Road, B Narayanapura, Bengaluru, Karnataka, India",
        "scheduledDate": "13-Feb-2015",
        "scheduledTime": "10:30 AM",
        "services": "2",
        "userContactNumber": "1220000000",
        "userName": "Gajendra"
    }
}

Myphp code for this

<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","1212");
mysql_select_db("service");

$query="Select * from customer where services='2'";
$result=mysql_query($query);

if ( $result === false ) {
  die("Can\'t do that: " . mysql_error());
}

$retVal = array();
//MYSQL_ASSOC remove key =field identifier
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
  $retVal[] = $row;
}
echo json_encode( $retVal );
Community
  • 1
  • 1
Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88
  • **WARNING** You're using a deprecated database API. Consider using [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – ʰᵈˑ Feb 12 '15 at 13:07

1 Answers1

3

By default mysql_fetch_array() returns an array containing all fields twice

a) key=field identifier
b) key=field position

just tell myqsl_fetch_array to put the values only under their identifier name into the array:

while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • But keep in mind: the mysql_* function are deprecated. Take a look at http://docs.php.net/manual/en/mysqlinfo.library.choosing.php – VolkerK Feb 12 '15 at 13:08
  • please see this question, I am not able to insert json object into which is produce this php code, http://stackoverflow.com/questions/15810257/create-nested-json-object-in-php – Neelabh Singh Feb 12 '15 at 13:11
  • please help me on this question http://stackoverflow.com/questions/28476038/how-to-insert-json-data-in-to-table – Neelabh Singh Feb 12 '15 at 13:29