3

I am using PHP 5.6.0 and connected to my local SQL Server. I was able to retrieve the data, but it is in an array format. I would like to convert it into a json format.

What I get:

(     
    [date] => 2013-02-05 16:02:02.000000
    [timezone_type] => 3
    [timezone] => America/New_York
)

What I want:

(
    "date" : "2013-02-05 16:02:02.000000",
    "timezone_type" : "3",
    "timezone" : "America/New_York"
)

Here is my code:

$sql = "SELECT * FROM table";
$stmt = sqlsrv_query($conn, $sql);

$result = array(); 

do {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
    $result[] = print_r($row); 
    }
} while (sqlsrv_next_result($stmt));


echo json_encode($result);


sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

My understanding is that json_encode should convert my data but it doesn't seem to work that way.

Thank you!

3
  • 1
    why are you using print_r($row)? Is that just a typo with some debugging? If not try it without the print_r Commented Sep 9, 2015 at 15:40
  • @Styphon I am using print_r because it's the only way I can actually see the code. without it, I get a blank screen. Commented Sep 9, 2015 at 15:46
  • Then you should use print_r($row); $result[] = $row;. What you currently have is invalid. Commented Sep 9, 2015 at 16:00

3 Answers 3

3

Your logic is quite unsual try it like this:

$sql = "SELECT * FROM table";
$stmt = sqlsrv_query($conn, $sql);

$result = array(); 

while ($row = mssql_fetch_array($stmt))
{
 array_push($result, $row);
} 

echo json_enconde($result);


sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank for this code but I am using SQL Server so I can't use mssql_fetch_array()
Yes you can! mssql_fetch_array is not mysql_fetch_array as seen here php.net/manual/en/function.mssql-fetch-array.php
I can't suggest an edit, but for anyone copying and pasting this, there's a typo in the line that starts with echo. It should be json_encode not json_enconde
1

I guess there were two mistakes in your code. First one is

$result[] = print_r($row); 

You are executing a function and pushing values in array at same time. You should push value in array here. Like

$result[] = $row;

And Second one is, not printing the JSON varibale after encoding it. So your code would be

$sql = "SELECT * FROM table";
$stmt = sqlsrv_query($conn, $sql);

$result = array(); 

do {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
       $result[] = $row; 
    }
} while (sqlsrv_next_result($stmt));


sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first

echo json_encode($result); //You will get the encoded array variable

2 Comments

Thank you for submitting @Vineet but this still didn't help for some reason. I got a blank screen.
call die() or exit() at the end of the your script.
1

Sql Server 2016 has FOR JSON clause that automatically transforms result set to JSON see Format Query Results as JSON with FOR JSON (SQL Server)

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.