Problem when verifying that my user is in the ORACLE database

2

Is there any kind of conflict with rowCount? The sentence I understand is fine, and I'm using PDO, which also did not give me problems before.

The field in the database is VARCHAR2.

I attach my code:

    $user = 'pepito'
    $con = connection();
    $statement = $con->prepare("SELECT name FROM usuarios WHERE name = :usu");
    $statement->bindParam(':usu', $user);
    $statement->execute();
    //Con este contador, devielve 1 si encuentra el usuario
    $contador = $statement->rowCount();

The counter returns 0, when I would have to return 1.

    
asked by Pelayo 13.02.2018 в 14:41
source

1 answer

3

A call to rowCount() after a query SELECT may return 0 instead of the number of records obtained depending on the database.

As you can read in the official documentation of PDOStatement::rowCount() :

  

PDOStatement::rowCount() returns the number of rows affected by the last statement DELETE , INSERT , or UPDATE executed by the corresponding object PDOStatement .

     

If the last SQL statement executed by the associated PDOStatement object was a SELECT statement, some databases could return the number of rows returned by that statement . However, this behavior is not guaranteed for all databases and should not be relied upon for portable applications.

So you should not trust it to know the number of records obtained, you must use the SQL query COUNT or obtain all records with PDOStatement::fetchAll() and make a count() to the result obtained.

    
answered by 13.02.2018 / 17:01
source