-1

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>
```
halfer
  • 19,824
  • 17
  • 99
  • 186
rubla
  • 1
  • 2
  • Your specifying columns in the query, so why would you need to count them? – Jay Blanchard Mar 04 '20 at 19:31
  • I need to show number of rows like result - in right of top corner of the table. When I use i++ i take result after generate the table, i can use it down but not in the top. So I don't know how to display it. – rubla Mar 04 '20 at 21:35
  • Hi there. We don't use `[Solved]` title hacks here. Please either accept an answer below (by ticking it) or write a self-answer and accept/tick that. Thanks! – halfer Mar 19 '22 at 22:39

1 Answers1

0

you can use rowCount

$row_count = $result->rowCount();

or

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

Ronak Dhoot
  • 2,322
  • 1
  • 12
  • 19
  • I try it, but It doesn't work. – rubla Mar 04 '20 at 21:36
  • You cannot return a number of columns with row count unless you count the number of items in the row. – Jay Blanchard Mar 05 '20 at 01:06
  • Looks it's work `$row_count = count($result->fetchAll(PDO::FETCH_ASSOC));`, but when I use it, variable $result going to change and next table it's not generating. Can you help me to solve this problem? – rubla Mar 05 '20 at 07:15
  • @rubla try using https://www.php.net/manual/en/language.oop5.cloning.php `$resultTemp = clone $result;` – Ronak Dhoot Mar 05 '20 at 15:14
  • I'm using `$resultTemp = clone $result;$row_count = count($resultTemp->fetchAll(PDO::FETCH_ASSOC)); ` and I get error 500... – rubla Mar 05 '20 at 16:49
  • I'm doing strange thing. I'm using post twice. So one time I've count rows and second time I've made extract. So I achieved result... – rubla Mar 07 '20 at 20:48