猿问

在网络中使用 PHP 显示 SQL 服务器的结果?

我在这里询问如何将MySQL查询转换为SQL服务器,因为这将是我第一次尝试这样做。我已成功创建与 SQL Server 的连接,但问题是从表中提取数据。

我还在这里阅读了一些与我类似的问题中提供的一些解决方案

从 SQL 服务器中选择数据,以及如何从 SQL 服务器获取数据

以下是我的代码:

数据库.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));

}

以下是我试图从 SQL 服务器数据库调用数据的代码


查看.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>

这是已经回响给我的错误

注意:尝试获取 C:\xampp\htdocs\snapper\用户\仪表板中非对象的属性“num_rows.php第 121 行


大话西游666
浏览 159回答 1
1回答

慕仙森

请尝试如下操作我试过了,它的工作原理<div class="col-md-6">&nbsp; <div class="info-box bg-c-yellow">&nbsp; &nbsp; <span class="info-box-icon bg-red"><i class="fa fa-chart-bar"></i></span>&nbsp; &nbsp; <div class="info-box-content">&nbsp; &nbsp; &nbsp; <span class="info-box-text">Count of Near Miss</span>&nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp;$sql = "SELECT COUNT (*) incident_type FROM iir_incidentmain WHERE incident_type='Accident'";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $stmt = sqlsrv_query( $conn, $sql );&nbsp; &nbsp; &nbsp; &nbsp;$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;?>&nbsp; &nbsp; &nbsp; &nbsp; <span class="info-box-number"><?php echo $row['incident_type']?></span>&nbsp; &nbsp; </div>&nbsp; </div></div>
随时随地看视频慕课网APP
我要回答