-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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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 );
1
  • WARNING You're using a deprecated database API. Consider using PDO or MySQLi. Commented Feb 12, 2015 at 13:07

1 Answer 1

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 ) ) {
Sign up to request clarification or add additional context in comments.

3 Comments

But keep in mind: the mysql_* function are deprecated. Take a look at docs.php.net/manual/en/mysqlinfo.library.choosing.php
please see this question, I am not able to insert json object into which is produce this php code, stackoverflow.com/questions/15810257/…
please help me on this question stackoverflow.com/questions/28476038/…

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.