'pdo mssql fetchColumn() [duplicate]

I need to count how many colums I've been select. Now I do It with reapeat i++, but I need use number of selected row up on site, but i can't.

I tried to use fetchAll, but without possitive result ($table_fields = $result->fetchAll(PDO::FETCH_COLUMN); print_r($table_fields);) Can somebody help me please?

I'm using IIS and MSSQL 2012...

    $query2 = "SELECT
      SURNAME_USER,
      FIRST_NAME_USER,
      CR_FACTORY,
      CR_LICENC,
      EVENT_NAME,
      EVENT_DATE_FROM,
      EVENT_DATE_TO
    FROM dbo.EVENT_CALENDAR_EVENT
    join EVENT_CALENDAR_CAR on OBJECT_CAL_ID = CAL_OBJECT_ID
    join AA_USER on ID_USER=EVENT_USER_ID
    where (SURNAME_USER+' '+FIRST_NAME_USER) like ? and EVENT_DATE_FROM >= ? and EVENT_DATE_TO <= ? order by EVENT_DATE_FROM desc";
      $result = $conn->prepare($query2);
      $result->bindValue(1, $user_name, PDO::PARAM_STR);
      $result->bindValue(2, $datum_od, PDO::FETCH_NUM);
      $result->bindValue(3, $datum_do, PDO::FETCH_NUM);

      $result->execute();
      $count=1;

?>
<div class="col-md-3 mb-3">
<p>Filtr</p> 
<input id="myInput" type="text" class="form-control" placeholder="Hledej...">
<br>
</div>
<div>
  <table class="table table-hover">
      <t>
          <th scope="col">Počet cest</th>
          <!--<th scope="col">Jméno</th>
          <th scope="col">Přijmení</th>-->
          <th scope="col">Udalost</th>
          <th scope="col">Automobil</th>
          <th scope="col">Datum od</th>
          <th scope="col">Datum do</th>
      </t><tbody id="myTable">
  <?php foreach($result as $rows) {
          ?>
          <tr>
              <td scope="row"><?php echo $count;?></td>
          <!--    <td><?php echo $rows["FIRST_NAME_USER"];?></td>
              <td><?php echo $rows["SURNAME_USER"];?></td> -->
              <td><?php echo $rows["EVENT_NAME"];?></td>
              <td><?php echo $rows["CR_FACTORY"]." ".$rows["CR_LICENC_PLATE"];?></td>
              <td><?php echo date('d-m-Y', strtotime($rows["EVENT_DATE_FROM"]));?></td>
              <td><?php echo date('d-m-Y', strtotime($rows["EVENT_DATE_TO"]));?></td>
          </tr>

<?php
$count++;
  }?></tbody></table>
```


Solution 1:[1]

you can use rowCount

$row_count = $result->rowCount();

or

$row_count = count($result->fetchAll(PDO::FETCH_ASSOC));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1