2

I am a .NET developer and recently i started to learn php. I want to use php for creating basic API's. However, i couldn't really find a proper way of doing it.

So here is my sample code

<?php
header('Access-Control-Allow-Origin: *');
$conn = sqlsrv_connect( "ip", array("Database"=>"dbnane", "UID"=>"uid", "PWD"=>"123456")) or die("Couldn't connect");
$arr = array();
if( $conn ) {
     $result = sqlsrv_query($conn, "SELECT * FROM Table");
     while( $obj = sqlsrv_fetch_object( $result )) {
         $arr[] = $obj;
     }
     echo json_encode($arr);
}
sqlsrv_close($conn);
?>

I wanted put the rows in an array, as an object and encode that array to json. However, this prints nothing and i don't get any errors. What am i doing wrong ? In other way, i might create a data transfer object/class for the requested table and put that in the array. That might possibly work but this method, if doable, looks more dynamic and clean.

Edit: Just to make it clear, query doesn't fail. I can print out the values with

         while( $obj = sqlsrv_fetch_object( $result )) {
              echo $obj->ColName ."<br/>";
        }

Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Arefi Clayton
  • 841
  • 2
  • 10
  • 19
  • Probably your query fails. Since you do not do any error detection/handling at all you blindly assume you actually _have_ objects, which might not be the case. I suggest you take a look into your error log file to read about the cause. – arkascha Nov 01 '15 at 10:07
  • @arkascha Query doesn't fail. I can get the values inside with `$obj->ColName`. – Arefi Clayton Nov 01 '15 at 10:10
  • Have you tried `var_dump($arr);` after the while loop? Does it output anything? – Jan Nov 01 '15 at 10:14
  • Maybe the json extension is not loaded? But that should give an error in the log file too, which you claim does not happen. Strange. Then all that is left is your output. _Where_ do you output to? – arkascha Nov 01 '15 at 10:18
  • Maybe the json extension is not loaded and error reporting if off. – anwerj Nov 01 '15 at 10:22
  • Not sure but maybe the problem connected with the encoding. Take a look at [this](http://stackoverflow.com/questions/19361282/why-would-json-encode-returns-an-empty-string) question. – Avag Sargsyan Nov 01 '15 at 10:25
  • I've found the problem but couldn't find a solution. The problem is one of the members of my object which is a UTF8 string doesn't recognized as a UTF8 by `json_encode()`. Not really sure why. Because, when i use `mb_detect_encoding()` on the string element of the array, it outputs UTF8. I am kinda lost right now. – Arefi Clayton Nov 01 '15 at 18:17

2 Answers2

0

The answer is quite simple, if as you claim the query is producing output, the last thing you are actually echoing is probably not the result of the json_encode() so move that to be that last thing you do before exiting.

<?php
    header('Access-Control-Allow-Origin: *');
    $conn = sqlsrv_connect( "ip", array("Database"=>"dbnane", "UID"=>"uid", "PWD"=>"123456")) or die("Couldn't connect");
    $arr = array();
    if( $conn ) {
         $result = sqlsrv_query($conn, "SELECT * FROM Table");
         while( $obj = sqlsrv_fetch_object( $result )) {
             $arr[] = $obj;
         }
    }
    sqlsrv_close($conn);
    echo json_encode($arr);
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

I've fixed my problem. The problem is one of the members of my object which is an UTF8 string doesn't recognized as an UTF8 by json_encode();. In this case, 2 things you should be looking at.

First, check the document encoding. In my case, my document saved by Notepad++ encoded in ANSI. I've changed that with opening it up with notepad and changed the encoding to UTF8.

Second, i've used json_encode() with an extra parameter which is available for PHP 5.4 and above, JSON_UNESCAPED_UNICODE. So i've used it like json_encode($arr,JSON_UNESCAPED_UNICODE).

and poof. Everything worked like a charm.

I've found it here

Thanks everyone.

Community
  • 1
  • 1
Arefi Clayton
  • 841
  • 2
  • 10
  • 19