Would someone be able to give me a MySQL relation query for looking up a column value of a table based off of another value of the same table?
For instance, I have a table with 4 columns (id, name, email, password). How could I look up the the value of the “id” column of a certain user based off of their email in the “email” column and store the result (id) in a variable?
Here’s the session() controller function
function userIsLoggedIn()
{
if (isset($_POST['action']) and $_POST['action'] == 'login')
{
if (!isset($_POST['email']) or $_POST['email'] == '' or
!isset($_POST['password']) or $_POST['password'] == '')
{
$GLOBALS['loginError'] = 'Please fill in both fields';
return FALSE;
}
$password = md5($_POST['password'] . 'chainfire db');
if (databaseContainsAuthor($_POST['email'], $password))
{
session_start();
$_SESSION['loggedIn'] = TRUE;
$_SESSION['email'] = $_POST['email'];
$_SESSION['password'] = $password;
$_SESSION['authorid'] = $author;
return TRUE;
}
else
{
session_start();
unset($_SESSION['loggedIn']);
unset($_SESSION['email']);
unset($_SESSION['password']);
unset($_SESSION['authorid']);
$GLOBALS['loginError'] =
'The specified email address or password was incorrect.';
return FALSE;
}
}
I’ve tried
SELECT id FROM article WHERE email='$email'
Is there a more efficient way to do it? I’m also not totally sure how to store the result of the query in the $author session variable.
Thanks in advance.
Your query is perfect (with the proviso that you are sanitising the
$emailvariable – see the tale of Little Bobby Tables).And to perform that query and set the variable
$authorto the returned value is simply:Rewritten in long form, if the above is too complex to follow: