PDO - Which is better: columnCount or rowCount? [closed]

1

I'm new to PDO (I was forced to choose between mysqli and pdo after migrating php from version 5.2 to 7.0) and I made a system where the user searches:

$b2 = $_POST['b2'];
$busca = $PDO->query("SELECT * FROM noticias WHERE titulo LIKE '%$b2%' UNION SELECT * FROM noticias WHERE corpo LIKE '%$b2%'");

$total->columnCount();

OR

$total->rowCount();

In the end it will be used to display the following information:

A busca retornou <?php echo $total ?> resultados;
    
asked by anonymous 20.11.2017 / 01:11

2 answers

0

It's not a question of "what's the best", are functions with different purposes.

According to the PDO documentation:

  

columnCount - Returns the number of columns obtained in the query result

and

  

rowCount - Returns the number of rows affected by the SQL statement

rowCount will only return something if you do DELETE, INSERT, or UPDATE. Some databases may return the registration number if you just make a SELECT, but it is not guaranteed.

As fetchAll returns an array, you can simply use count to check the number of results

count($busca->fetchAll())
    
20.11.2017 / 01:29
0

rowCount() and columnCount() have purposes.

rowCount() returns the number of rows affected by an INSERT / UPDATE / DELETE that is the equivalent of mysqli_affected_rows() .

Because the PDO supports multiple databases in some like MySQL this method also returns the number of rows in a SELECT as well as mysqli_num_rows() ". Some other settings are required in other databases.

columnCount() Returns a number of columns of a SELECT

Relates to:

SQL SERVER - PDO :: rowCount () Returning -1

    
20.11.2017 / 17:36