Reputation: 140
I've got a database with 3 columns, "ID", "username", "password". I'm pretty new on SQL (still learning :P ), and well, I'm not able to get my idea working. This is for an Android app. I've made an login, you enter the login data and I've arranged everything for working but this:
I need a SQL query that retrieves ID of the account ONLY IF username and password matches. This is what I've so far:
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$hostname = "-----";
$dbuser = '-----';
$dbpass = '-----';
$dbname = '-----';
//connection to the database
$dbhandle = mysql_connect($hostname, $dbuser, $dbpass) or die("Unable to connect to MySQL " . \mysql_error() . "<br>");
//select db
mysql_select_db($dbname, $dbhandle) or die("Unable to connect to DB: " . \mysql_error() . "<br>");
$result = mysql_query("SELECT usr_id IF (username = '$username' AND password = '$password') FROM people") or die("Error: " . mysql_error());
echo $result;
?>
I know Google is my friend, but when I've tried to find it out, the references just gave me non-relevant information, or at least, not relevant for this case.
So, anyway, thank for your time!
Upvotes: 0
Views: 517
Reputation: 369
$result = mysql_query("SELECT usr_id FROM people WHERE username = '$username' AND password = '$password');
$result_array=mysql_fetch_assoc($result);
echo $result_array['usr_id'];
Upvotes: 0
Reputation: 664
Try using WHERE instead of IF, so:
"SELECT usr_id FROM people WHERE username = '".$username."' AND password = '".$password."'"
Upvotes: 1
Reputation: 263743
use WHERE
clause to filter such conditions,
SELECT usr_id
FROM people
WHERE username = '$username' AND password = '$password'
Upvotes: 0