Lance Hietpas Lance Hietpas - 7 months ago 22
PHP Question

Use session variable in PDO MSSQL Statement to get another column

I'm trying to learn more about PHP, PDO and SQL and session variables. I'm sure there is a better way to do this, but I'm hoping to learn from what I'm attempting.

I'm grabbing a session variable that is an e-mail and want to use that variable in a PDO statement to grab a ID that is in the same table. Below is what I have so far. Thanks!

<?php
require_once ('PasswordVerifySession.php');

$dsn = 'sqlsrv:Server=.database.windows.net;Database=';
$uname = '';
$pwd = '';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$ticketNum = $_POST["txtTicket"];
$Email = $_SESSION["contEmail"]
try {
$db = new PDO($dsn, $uname, $pwd, $options);
$q = $db->prepare("SELECT contId FROM tblContestants WHERE Email = ????);
$q->bindValue(':contId', $contID);
$q->execute();
$ContId = $q->fetch();
$ContId = $ContId['contId'];
}
catch (PDOException $e) {
$error_message = $e->getMessage();
echo("<p>Database error: $error_message</p>");
exit();

}


?>

Answer

I suggest you take just a quick look at the manual

$Email = $_SESSION["contEmail"]
try {
    $db = new PDO($dsn, $uname, $pwd, $options);
    $q = $db->prepare("SELECT contId FROM tblContestants WHERE Email = :email");
    $q->bindValue(':email', $Email);
    $q->execute();
    $row = $q->fetch(PDO::FETCH_ASSOC);
    $ContId = $row['contId'];
}