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