How to pass the value of a "$variable" coming from another page, and use it in a select PDO

Asked

Viewed 57 times

0

Next, first I would like to know if it is possible to receive from another page, through a session_start(), value of a variable and apply it in a select.

I’m doing it this way:

At the beginning of the page where I want to add select, I added the session_start(). Right after, I added the command echo $_SESSION['user'] further down on the page next to HTML and so on, all right, printed the contents of the screen variable.

Just below in HTML comes select:

select unidade from unidades_administrativas A
join users B on A.id = B.id_unidades
where B.user = '{COMO PASSAR O VALOR de $_SESSION['user'] AQUI???}'

Note: The purpose of this select is to display the unit of the user that is logged in to the system.

So my question is: How to pass the value of this variable to select using PDO?


The following errors occurred after using your code:

Pdoexception: SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '= 'User 1'' at line 3 in C:wamp www point_electronica add_txt_list_drives.php on line 321

  • Pass by $_SESSION['user'] he of the Undefined index?

  • It simply does not return anything, nor error message.

1 answer

0

try the following:

$consulta = $pdo->prepare("select unidade from unidades_administrativas A
join users B on A.id = B.id_unidades
where B.user = :usuario;");
$consulta->bindParam(':usuario', $_SESSION['user'], PDO::PARAM_STR);
$consulta->execute();
$linha = $consulta->fetch(PDO::FETCH_ASSOC);
  • Christopher, the following errors occurred after using your code: Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '= 'User 1'' at line 3' in C: wamp www ponto_eletronico add_txt_list_unidades.php on line 321

  • it seems to me that ended up getting the two signs of = = , please remove one before the :user

  • "User 1" is your search key? Avoid blank spaces and echo and post like this getting your syntax before the run to better evaluate.

  • Christopher, , after your tips, I realized I was using the wrong variable! When running select, I used $_SESSION['user_name'] instead of $_SESSION['user']. So I was returning {User 1}. With $_SESSION['user']. returned = user1. ( without spaces).

  • Christopher, , after your tips, I realized I was using the wrong variable! When running select, I used $_SESSION['user_name'] instead of $_SESSION['user']. So I was returning {User 1}. With $_SESSION['user']. returned = user1.( no spaces). I believe that now select this ok. Wanted to test it by printing the variable on the screen through a while(if the user1 is registered in more than one unit) <? php while ($line = $query->fetch(PDO::FETCH_ASSOC)) { echo "UNIT:". $line['A.unit']; } ? > So that returns nothing!

  • SOLVED! Thanks for the help!

Show 1 more comment

Browser other questions tagged

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