I'm here to ask about how to convert MySQL query to SQL Server since this will be my first attempt on doing it. I have successfully create the connection to my SQL Server but to problem is to fetch the data from the table.
I have also read some solutions as provided from similar questions to mine here
Selecting data from SQL Serverand here how to fetch data from sql server
Following is my code:
db.php
<?php
//Your sql Config
$servername = "SNAPPER";
$connectionInfo = array ("Database"=> "CDS", "UID"=>"admin", "pwd" =>"mypassword");
//Create New Database Connection
$conn =sqlsrv_connect($servername, $connectionInfo);
//Check Connection
if($conn){
//echo "Connection Established";
}else {
echo "Connection fail";
die (print_r(sqlsrv_errors(), true));
}
and here is the code that I tried to call for the data from the SQL Server database
view.php
<div class="col-md-6">
<div class="info-box bg-c-yellow">
<span class="info-box-icon bg-red"><i class="fa fa-chart-pie"></i></span>
<div class="info-box-content">
<span class="info-box-text">Count of Accident</span>
<?php
$sql = "SELECT * FROM iir_incidentmain WHERE incident_type='Accident'";
$result = sqlsrv_query($conn, $sql);
if($result->num_rows > 0) {
$totalno = $result->num_rows;
} else {
$totalno = 0;
}
?>
<span class="info-box-number"><?php echo $totalno; ?></span>
</div>
</div>
</div>
and here is the error that has been echoed to me
Notice: Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\snapper\user\dashboard.php on line 121
mysqlicode andmssql. Maybe$rows = sqlsrv_has_rows( $result ); if ($rows === true)or php.net/manual/en/function.sqlsrv-has-rows.php would be better