I am starting to learn PHP and will need to connect to SQL Server. The web server I am using is Apache. My PHP below outputs the "Connection established" in the browser window okay but to make sure I am getting data too I included the code for getting a row count but for some reason the code around the row count doesn't appear to work but yet the code after the row count part outputs the "Results returned" to the browser. I have tried changing the SELECT to a count using COUNT(Name) and also changed
$result = sqlsrv_query($conn, $query)
to
$result = sqlsrv_query($conn, $query, array(), array("Scrollable" =>
'static'))
But neither has worked so far. Why is the row count not working?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Check SQL Server Connection</title>
</head>
<body>
<?php
$serverName = "IT90334\SQLEXPRESS";
$connectionInfo = array('Database' => 'AdventureWorks');
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn) {
echo "Connection Established.<br />";
} else {
echo "Something went wrong while connecting to MSSQL.<br />";
die(print_r(sqlsrv_errors(), true));
}
$query = "SELECT Name FROM production.Location"
$result = sqlsrv_query($conn, $query)
or die('An error has occurred');
$rowcount = sqlsrv_num_rows($result);
if($rowcount === FALSE){
echo "failure";
}
else {
echo $rowcount;
}
if($result === FALSE){
die(print_r(sqlsrv_errors(), true));
}
else {
echo "Results returned. <br />;
}
sqlsrv_close($conn);
?>
</body>
</html>