Im trying to fetch information from a online hosted database and convert it to json, i have the php script that i made but when i execute it, it displays a blank page where it should show the data from the database.
Here is the script:
<?php
define('HOST','---');
define('USER','---');
define('PASS','---');
// define('DB','plofenosa');
$con = mysqli_connect(HOST,USER,PASS);
if (!$con)
{
die ('Could not connect:' .mysqli_connect_error());
}
mysqli_select_db("plofenosa", $con);
$result = mysqli_query("SELECT * FROM Obras");
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print json_encode($output);
mysqli_close($con);
?>
UPDATED:
<?php
define('HOST','mssql3.gear.host');
define('USER','plofenosa');
define('PASS','Xn6g_1s_IqFc');
define('DB','plofenosa');
$con = mysqli_connect(HOST,USER,PASS);
if (!$con)
{
die ('Could not connect:' .mysqli_connect_error());
}
mysqli_select_db("plofenosa", $con);
$result = mysqli_query("SELECT * FROM Obras");
$output = array(); //Added new line
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print json_encode($output);
mysqli_close($con);
?>
UPDATE #2
<?php
$serverName = "---";
/* Get UID and PWD from application-specific files. */
$uid = 'plofenosa';
$pwd = '---';
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>"plofenosa");
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo 'Connected successfully';
$tsql = "SELECT * FROM Obras";
$stmt = sqlsrv_query($conn, $tsql);
if( $stmt === false ) {
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
$json = array();
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$json[] = $row;
}
} while ( sqlsrv_next_result($stmt) );
/* Run the tabular results through json_encode() */
/* And ensure numbers don't get cast to trings */
print json_encode($json,<code> JSON_NUMERIC_CHECK</code>);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>