I'm trying to get data from a Microsoft SQL database using PHP which the PHP is based on a Raspberry PI running Apache2 and the database on a separate server. From the Raspberry PI I can connect to the server OK but when I try using PHP to get the data to display as a web page I get nothing and also no errors
SQL code for set up (image of existing table should be attached):
sql set up
CREATE TABLE [dbo].[statuslabel](
[id] [int] IDENTITY(1,1) NOT NULL,
[status_label_value] [varchar](50) NOT NULL,
PRIMARY KEY CLUSTERED
PHP code:
<!DOCTYPE html>
<html land="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width-device-width, initial-scale=1.0">
<title>Test SQL page</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<h1>Test page, SQL connection</h1>
<br>
<?php
/*set up connection*/
$serverName = "ipaddress:port number"; //serverName\instanceName, portNumber (1433 by default)
$connectionInfo = array( "Database"=>"databasename", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo) or die("Unable to Connect to database");
if ($conn === false){
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$tsql = "SELECT [id],[status_label_value] FROM [dbo].[statuslabel]";
$stmt = sqlsrv_query($conn, $tsql) or die("SQL query error");
if( $stmt === false){
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
while($obj = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
echo $obf['status_label_value'].'</br>';
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
</body>
</html>
I have looked though various example on this site and tried the answers I understood and could adapt but not success. I have also tried mysqli code as well as sqlsrv and both seem to return blank tables. Connection to server tests OK based on IP address and port number and I have tried to add error code but nothing is pointing my in the right direction. If I try to connect to a MariaDB hosted locally on the Raspberry PI I can fetch the data so I am wondering if:
- I cannot get data from a different SQL server or
- The Raspberry PI OS running apache and PHP can't get the data from the Mircosoft server.
I have tried adding the Mircosoft driver to the Pi but I get msodbcsql18 unavailable (same for msodbcsql17) and unixodbc but still no luck.