How to obtain the line number of a SELECT statement in PDO?

Asked

Viewed 1,192 times

0

How do I know how many records I have returned from a SELECT statement? I tried using rowCount() but this did not work in my code, I just want to return to me the amount only this.

$contador = $this->con->conectar()->prepare("SELECT * FROM login WHERE 'usuario' =:usuario AND 'senha' =:senha");
        $contador->bindPARAM(":usuario", $this->usuario, PDO::PARAM_STR);
        $contador->bindPARAM(":senha", $this->senha, PDO::PARAM_INT);
        $contador->execute();

        echo $contador->rowCount();
  • $number = $counter->rowCount(); echo $number;

  • Did not work with this code Everton it shows result 0, and in the BD contains the user and password.

  • In this case you don’t even need, how many lines can you have with the same username and password? If there are more than um rethink your table!

  • SELECT * FROM login WHERE 'usuario' =:usuario AND 'senha' =:senha - It shouldn’t just be SELECT * FROM login WHERE usuario = :usuario AND senha = :senha? That is, without the apostrophes?

  • I tried so and also did not work SELECT * FROM login WHERE usuario =:user AND password =:password, I do not know where I am missing

  • missing $ in variables where user =:$user AND password =:$password

  • Read this post https://answall.com/questions/87384/qual-a-diferen%C3%A7a-entre-bindparam-e-bindvalue

Show 2 more comments

3 answers

1

It may be that he is not understanding your request, review the SQL syntax just for granted, if it doesn’t work try to do it this way:

// Cria objeto PDO
$conexao = new PDO('mysql:host=localhost;dbname=meuBanco', 'root', 'senhasenha');

// Query que será executada. Recebe o parâmetro :email
$query = "select * from login where usuario= :usuario AND senha = :senha";

// Prepara a query para execução
$consulta = $conexao->prepare($query, array(PDO::CURSOR_SCROLL));

// Atribui o parametro $email a :email na consulta
$consulta->bindParam(':usuario', $usuario,':senha',$senha);

// Executa a consulta ao banco de dados
$consulta->execute();

// Conta quantas linhas foram retornadas do banco de dados
$numero_linhas = $consulta->rowCount();
  • I tried it this way and it didn’t work either. public Function Login($user, $password) { if(filter_input(INPUT_POST, "boot") === "Employee"){ $query = "SELECT * FROM login where user =:user AND password =:password"; $query = $this->con->connect()->prepare($query, array(PDO::CURSOR_SCROLL)); $query->bindPARAM(":user", $this->user); $query->bindPARAM(":password", $password); $query->execute(); $num_lines = $query->rowCount(); echo $num_lines;

1

I believe that rowCount() only works well with UPDATE, DELETE and INSERT

In the manual >> rowCount() - returns the number of rows affected by the last DELETE, INSERT or UPDATE statement executed.

Use Pdostatement :: fetchColumn() to recover the number of rows that will be returned.

$hostname="localhost";  
$username="USUARIO";  
$password="SENHA";  
$db = "NOME_DB";  
$conexao = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);

$nRows = $conexao->query("select count(*) from login where usuario='$usuario' and senha='$senha'")->fetchColumn(); 
  • Unfortunately it did not work using fetchColunm, I no longer know where to search on the internet to get the number of lines of a SELECT in PDO, I am not using query use prepare and then run

  • @sol25lua, I can post the code you used as I suggested in my reply, because here I tested in my bank and it worked?

  • public Function Login($user, $password) { if(filter_input(INPUT_POST, "boot") === "Employee"){ $query = $this->con->connect()->prepare("SELECT * FROM login where user =:user AND password =:password")->fetchColunm(); $query->bindPARAM(":user", $->user); $query->bindPARAM(":password", $this->$password); $query->execute(); } }

  • I used this way you suggested me but I am using prepare and no query

  • Fatal error: Call to Undefined method Pdostatement::fetchColunm() in C: xampp htdocs Programas_orientacao_obj Casa_das_marmitas Logar.php on line 19

  • you are mixing, do as the codes are in the answers

Show 1 more comment

0

Try this way.

    $contador = $this->con->conectar()->prepare("SELECT * FROM login WHERE 'usuario' = :usuario AND 'senha' = :senha");
    $contador->bindValue(":usuario", $this->usuario);
    $contador->bindValue(":senha", $this->senha);
    $contador->execute();

    $numero = $contador->rowCount();
    echo $numero;
  • 1

    It didn’t work he didn’t count

  • What result does it return?

  • 0 but has resulted in database

Browser other questions tagged

You are not signed in. Login or sign up in order to post.