1

I try to count the event-code. What are the changes I need to do to achieve it?
Please help me with that.

My code:

$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
  • 1
    What isn't working? Commented Mar 1, 2018 at 12:03
  • you should give the column a name with count(EventCode) AS EventCodeCount and then you can use $row['EventCodeCount'] Commented Mar 1, 2018 at 12:07

2 Answers 2

2

Do some change here

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

and here

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
  echo $row['total_event']."<br>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

I glad that I am of your help
1

You will set a alies name for count,

$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);

?> 

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.