Solution 1 :

Please try as the following

I tried and it worked

<div class="col-md-6">
  <div class="info-box bg-c-yellow">
    <span class="info-box-icon bg-red"><i class="fa fa-chart-bar"></i></span>
    <div class="info-box-content">
      <span class="info-box-text">Count of Near Miss</span>
      <?php
       $sql = "SELECT COUNT (*) incident_type FROM iir_incidentmain WHERE incident_type='Accident'";
                      
		$stmt = sqlsrv_query( $conn, $sql );
		 $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC); 
       ?>
        <span class="info-box-number"><?php echo $row['incident_type']?></span>

    </div>
  </div>
</div>

Problem :

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:xampphtdocssnapperuserdashboard.php on line 121

Comments

Comment posted by Mech

You did include / require the “db.php” file in “view.php”, right?

Comment posted by WanHazyan

@Mech yes I do. I did include db.php. I just shows part of the source code

Comment posted by Mech

what’s dashboard.php, line 121

Comment posted by php.net/manual/en/function.sqlsrv-has-rows.php

Looks like merging

Comment posted by WanHazyan

@user3783243 I see. Well this is because this will be my first time using SQL Server and I’m novice about it. Thank you for pointing it out for me

By