Blaine Hurtado
Blaine Hurtado

Reputation: 139

Passing PHP Session variable through MySQL command in PDO

I have the following code in PHP. I am trying to pass the log-in value from the session variable into my SELECT command in my database. The ? represents the log-in created in the session. :

try {    
    $conn = new PDO(A_DB_HOST, A_DB_USER, A_DB_PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$member['login'] = $_SESSION['SESS_login'];
$qry=$conn->prepare("SELECT * FROM {?}_clients WHERE login=?");
$qry->bindParam(1, $_SESSION['SESS_login']);
$qry->execute();

Currently, I am getting the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in /home/content/14/9957114/html/agent-member-index.php:15 Stack trace: #0 /home/content/14/9957114/html/agent-member-index.php(15): PDOStatement->execute() #1 {main} thrown in /home/content/14/9957114/html/agent-member-index.php on line 15

Any help is greatly appreciated!

Upvotes: 0

Views: 641

Answers (1)

deceze
deceze

Reputation: 522195

  1. You cannot bind values to identifiers like table names, only to values.
  2. You have two ? placeholders in that query but are only binding one value.

Upvotes: 2

Related Questions