如何使用php打印计数sql查询
我尝试计算事件代码。我需要做哪些改变才能实现它?如何使用php打印计数sql查询,php,Php,我尝试计算事件代码。我需要做哪些改变才能实现它? 请帮帮我 我的代码: $serverName = "IE3PDT1QJ67P4"; $connectionInfo = array( "Database"=>"TestEventsDB"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; } else{
请帮帮我 我的代码:
$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);
?>
$serverName=“IE3PDT1QJ67P4”;
$connectionInfo=数组(“数据库”=>“TestEventsDB”);
$conn=sqlsrv_connect($serverName,$connectionInfo);
如果($conn){
回显“已建立连接。
”;
}
否则{
echo“无法建立连接。
”;
模具(打印错误(sqlsrv_errors(),true));
}
$sql=“从AlarmPointEvent中选择计数(EventCode),其中AlarmPoint=24001”;
$stmt=sqlsrv_查询($conn,$sql);
如果($stmt==false){
模具(打印错误(sqlsrv_errors(),true));
}
而($row=sqlsrv\u fetch\u数组($stmt,sqlsrv\u fetch\u ASSOC)){
echo$row['EventCode']。“
”;
}
sqlsrv_free_stmt($stmt);
?>
在这里做一些更改
$sql = "SELECT count(EventCode) as total_event FROM AlarmPointEvent where AlarmPoint=24001";
这里呢
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row['total_event']."<br>";
}
while($row=sqlsrv\u fetch\u数组($stmt,sqlsrv\u fetch\u ASSOC)){
echo$row['total_event']。“
”;
}
您将为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);
?>
$serverName=“IE3PDT1QJ67P4”;
$connectionInfo=数组(“数据库”=>“TestEventsDB”);
$conn=sqlsrv_connect($serverName,$connectionInfo);
如果($conn){
回显“已建立连接。
”;
}
否则{
echo“无法建立连接。
”;
模具(打印错误(sqlsrv_errors(),true));
}
$sql=“从AlarmPointEvent中选择count(EventCode)作为eventcount,其中AlarmPoint=24001”;
$stmt=sqlsrv_查询($conn,$sql);
如果($stmt==false){
模具(打印错误(sqlsrv_errors(),true));
}
而($row=sqlsrv\u fetch\u数组($stmt,sqlsrv\u fetch\u ASSOC)){
echo$row['eventcount']。“
”;
}
sqlsrv_free_stmt($stmt);
?>
什么不起作用?您应该给列起一个名,名为count(EventCode)AS EventCodeCount
,然后您可以使用$row['EventCodeCount']
我很高兴能得到您的帮助