Refer to PHP PDO user

Asked

Viewed 105 times

-7

For those with the doubt as I had, below I’ll leave the code ready, just change to your project. The code is simply to know the user logged in and say his name in PHP PDO. If your project is by cookie dara certain, if it is Session adapt your code to my.

//USERNAME

$sql= "SELECT * FROM nomedasuatabela WHERE email='$login_cookie'"; 
$stmt = $conexao->prepare($sql);
$stmt->bindParam(':email', $login_cookie, PDO::PARAM_INT); 
$stmt->execute();
  • 4

    If you want to answer the question itself no problem, just separate the text of the question and the answer in the respective fields, now question and answer only in the area/field of "question" is not valid. There is an error there in your code you cannot associate a bind if it does not exist in the SQL statement. The site works different from a forum see the differences in tour if you have any questions let us know, you can call a specific user like this @nomeDoUsuario.

  • 2

    I am voting to close this question as out of scope because it is not a question per se, but a hint.

1 answer

1


Unfortunately the query you placed is not correct; I believe that what you want to do there is to take an email that is in the browser cookie and passing to this method.

The right way would be:

$sql= "SELECT * FROM nomedasuatabela WHERE email = :email"; 
$stmt = $conexao->prepare($sql);
$stmt->bindParam(':email', $login_cookie, PDO::PARAM_STR); 
$stmt->execute();

Or

$sql= "SELECT * FROM nomedasuatabela WHERE email = ?"; 
$stmt = $conexao->prepare($sql);
$stmt->bindParam(1, $login_cookie, PDO::PARAM_STR); 
$stmt->execute();

I’m taking into account that the email there is a string.

  • Yes it is and way I found, when user log in or automatic registration creates a cookie, by cookie I take the name in the database and show on screen, but thanks, I will edit my project.

  • I didn’t understand this "1". $stmt->bindParam(1, $login_cookie, PDO::PARAM_STR);

  • That one 1 that is placed there is to speak the parameter that is passed, in an order. Often when you do not pass by reference (:parametro) você pode colocar pela ordem que é chamado, por exemplo:

Você tem 2 parametros, então entraria:

$stmt->bindParam(1, $parametroUm, PDO::PARAM_STR)
$stmt->bindParam(2, $parametroDois, PDO::PARAM_STR)

Browser other questions tagged

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