Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:10:32+00:00 2026-06-02T00:10:32+00:00

I want to return a JSON object from a database (MyTable) which contains two

  • 0

I want to return a JSON object from a database (MyTable) which contains two columns – ID (int) and Name (string).

The script below produces a JSON like:

[{"ID":"0","Name":"John"},{"ID":"1","Name":"Doe"}]

You can see, that the ID is returned as a string. How can I accomplish it in a way that an ID would be returned as integer:

[{"ID":0,"Name":"John"},{"ID":1,"Name":"Doe"}]

I do not want to read and parse the result array from the ODBC since I want to use generic queries like:

SELECT * FROM TableXXX

Here is my script:

    //connection
    $connstr = "Driver={SQL Server Native Client 10.0};Server=" . $this->server . ";Database=" . $this->database . ";"; 
    $conn=odbc_connect($connstr, $this->user, $this->password);
    if (!$conn) {exit("Connection Failed: " . $conn);}

    //execute sql
    $rs=odbc_exec($conn,"SELECT ID, Name FROM MyTable");
    if (!$rs) {exit("Error in SQL");}
    //retreive data into array
    $data = array();
    $i=0;
    while( $row = odbc_fetch_array($rs) ) {
        $data[$i] = $row;
        $i++;
    } 
    odbc_close($conn);
    //output as json string
    echo json_encode($data);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:10:34+00:00Added an answer on June 2, 2026 at 12:10 am

    In general, there are no common data types in ODBC. It would be dependent on the individual ODBC drivers (and possibly different from every vendor if it were supported). I have not seen anything in the ODBC specification that describes “common” data type names. Most calls into ODBC do go through the ODBC driver manager, but I am not aware of any situations in which it would perform translation/modification of SQL statements being passed through it.

    It means, that ODBC driver would not return data types. If you want to return a JSON object conatining else than strings you would have to map the data individually column by column. You can retrieve data types of the field by odbc_field_type function.

    However you have to know that each database vendor returns different field names (e.g. varchar, nvarchar, ….).

    Then use something like (just in reverse order):

    $type_map = array(
    'varchar' => 'string',
    'char' => 'string',
    'text' => 'string',
    );
    
    $column_type = 'text'; // get from DB query
    $my_var = 'some string';
    $var_type = gettype($my_var);
    
    if(array_key_exists($column_type, $type_map) and
       $type_map[$column_type] == $var_type) {
       //proceed with insert of record
    }
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MySQL database, where I store the following BLOB (which contains JSON object)
I want to return a json object from the onreadystatechange = function(){}, but I
I just simply want to return a JSON object (using ajax) from my server
I want to return a private field from a remoted object but i get
I want to break down a JSON string into smaller objects. I have two
I want to get data from thisarray of json object : [ { outgoing_relationships:
Is this possible? I am creating a an object from a JSON string with
I am receiving some JSON object from the server, and I want to 'typecast'
I want to return the Model (data) of a controller in different formats (JavaScript/XML/JSON/HTML)
I have a WCF Service that return a json. I want to test it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.