0

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

7
  • You did include / require the "db.php" file in "view.php", right? Commented Feb 18, 2020 at 3:31
  • 1
    @Mech yes I do. I did include db.php. I just shows part of the source code Commented Feb 18, 2020 at 3:34
  • what's dashboard.php, line 121 Commented Feb 18, 2020 at 3:38
  • 1
    Looks like merging mysqli code and mssql. Maybe $rows = sqlsrv_has_rows( $result ); if ($rows === true) or php.net/manual/en/function.sqlsrv-has-rows.php would be better Commented Feb 18, 2020 at 3:53
  • 1
    @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 Commented Feb 18, 2020 at 3:55

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.