0

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);

?>
BugDroid777
  • 67
  • 1
  • 9
  • Check that you have error reporting turned on. It could be the script is dying because of an error. Also consider changing the `mysqli_*` calls to [PDO](http://php.net/manual/en/class.pdo.php). – Schlaus Sep 17 '16 at 19:14
  • This question is caused by simple typo error and should be closed, as title does not reflect the exact problem and could be confusing to further views. – Damaged Organic Sep 17 '16 at 20:09

2 Answers2

2

This syntax is incorrect

mysqli_select_db("plofenosa", $con);

it should be

mysqli_select_db($con, "plofenosa");

Note the connection variable is first not second.

If in doubt, you can always read the manual as a last resort

Or it may be simpler to use the connection to select the database

$con = mysqli_connect(HOST,USER,PASS, "plofenosa");

See the manual for mysqli_connect

and remove the mysqli_select_db() completely

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Still blank page,i just say its a mssql database,does that make any difference ? and its hosted on gearhost. – BugDroid777 Sep 18 '16 at 11:52
  • Oh for heavans sake. YES IT MATTERS – RiggsFolly Sep 18 '16 at 11:56
  • You cannot use an API designed to access MYSQL to access a SQLServer database – RiggsFolly Sep 18 '16 at 11:56
  • Its a different API [See the MSSQL manual](http://php.net/manual/en/book.mssql.php) – RiggsFolly Sep 18 '16 at 11:57
  • I am sorry, i updated the post with updated code, can you take a look ? It worked till the echo 'Connected successfully'; but after i added after that line it doesnt display anything when i run it, i uploaded here: http://plofenosascript.gear.host/getObras2.php – BugDroid777 Sep 18 '16 at 13:17
0

Use this code

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);
siddhesh
  • 563
  • 1
  • 9
  • 15