Validate PHP Login PDO MYSQL

Asked

Viewed 857 times

-1

Hello I’m starting with php already engaged in the world of PDO and I’m with a doubts in my code

I want to check if the login exists and is the same that was entered, if for returns "existing login" if there is no "login"

More all the time is returning false.

Example below

require_once("conexao.php");
$sql = $conn->query("SELECT nome, senha FROM login where nome=? and senha=?");
$resultado = $conn->prepare($sql);
$resultado->bindParam(1, $_POST['nome']);
$resultado->bindParam(2, $_POST['senha']);
$resultado->execute();
if($resultado->fetch(PDO::FETCH_ASSOC) == true):
echo"existe login";

else:
echo"não existe login";
endif;

Someone can help me by following the PDO rule clearly and informs me some examples with articles arguing on the subject.

1 answer

-1

Maybe it could be because of lack of beginTransaction() and of commit(). If not already, place a Trycatch and see if it shows any errors.

require_once("conexao.php");
$sql = $conn->query("SELECT nome, senha FROM login where nome=? and senha=?");
$conn->beginTransaction();
$resultado = $conn->prepare($sql);
$resultado->bindParam(1, $_POST['nome']);
$resultado->bindParam(2, $_POST['senha']);
$resultado->execute();
$con->commit();
if($resultado->fetch(PDO::FETCH_ASSOC) == true):
echo"existe login";

else:
$conn->rollBack();
echo"não existe login";
endif;
  • beginTransaction() could explain me a little more about this method?

  • It "disables automatic confirmation mode. While auto-commit mode is disabled, changes made to the database through the PDO object instance will not be confirmed until you complete the transaction by calling the Commit()" (PDO documentation), basically it executes the query and waits for Commit to apply the query in the database. I don’t know if it makes any difference to one Select, but according to my Professor it is always interesting to put in any kind of Query.

Browser other questions tagged

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