如何使用php打印计数的sql查询

时间:2018-03-01 12:02:08

标签: php

我尝试计算事件代码。为实现这一目标,我需要做些哪些改变? 请帮帮我。

我的代码:     

$serverName = "IE3PDT1QJ67P4"; 
$connectionInfo = array( "Database"=>"TestEventsDB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
 echo "Connection established.<br />";
}
else{
    echo "Connection could not be established.<br />";
    die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT count(EventCode)  FROM AlarmPointEvent where AlarmPoint=24001";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
  die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  echo $row['EventCode']."<br>";
}

sqlsrv_free_stmt( $stmt);

?>

2 个答案:

答案 0 :(得分:2)

在这里做一些改变

:new:col

在这里

   $sql = "SELECT count(EventCode) as total_event FROM AlarmPointEvent where AlarmPoint=24001";

答案 1 :(得分:1)

您将为count设置一个alies名称,

$serverName = "IE3PDT1QJ67P4"; 
$connectionInfo = array( "Database"=>"TestEventsDB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
 echo "Connection established.<br />";
}
else{
 echo "Connection could not be established.<br />";
 die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT count(EventCode) as eventcount  FROM AlarmPointEvent where AlarmPoint=24001";
 $stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
  die( print_r( sqlsrv_errors(), true) );
 }

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
 echo $row['eventcount']."<br>";
}

sqlsrv_free_stmt( $stmt);

?>