[RESOLVED] Check User Existence
Hi guys,
Problem's this: I can't work with sql in php...
So, I was wondering if anyone could help me out with a simple(?) user existence script.
DB: subscription
Table: users
Field: username
I need something like a function which returns true or false
This is the code that I already have, but it isn't working
Code:
<?
function querydb($query, $host, $user, $pass, $db)
{
$connid = @mysql_connect($host, $user, $pass) or die("Connection to the server failed");
@mysql_select_db($db) or die ("Connection to database failed");
$retval = @mysql_query($query) or die ("Query excecution failed");
return $retval;
}
function checkuser($username)
{
$username = strtolower($username);
$q = "SELECT * FROM users WHERE username = '$username'";
$ret = querydb($q, 'localhost', 'root', '', 'subscription');
while ($row = mysql_fetch_assoc($ret)) {
$myret = $row["username"];
}
if ($myret != ''){
//echo "Username is unavailable";
echo $myret;
}
else{
//echo "Username is available";
echo $myret;
}
}
?>
Thanks for any help
Re: [RESOLVED] Check User Existence
Is there any reason not to do it like so (as I have done in a project I am working on?)
PHP Code:
$res = mysql_query("SELECT DISTINCT `mcs_users`.* FROM `mcs_users` WHERE `mcs_users`.`name` = '$name' AND `mcs_users`.`password` = SHA1('$password')");
if (!$res || mysql_num_rows($res) == 0)
{
// login failed
}
else
{
// store user info in session
}
Re: [RESOLVED] Check User Existence
Guess that would work too,
but as both work, what does it matter?
Re: [RESOLVED] Check User Existence
Quote:
Originally Posted by TheBigB
Guess that would work too,
but as both work, what does it matter?
I was just wondering if there was something insecure or iffy about the method I used, since I don't have that much experience with database/web design.
Re: [RESOLVED] Check User Existence
It's OK. However, I can't tell, from looking at the code, whether you've escaped the variables in the SQL or not. That is bad; unescaped data is a SQL injection vulnerability. You should use parameterised queries to avoid this. On PHP 4, look into MDB2; on PHP 5, use PDO, or mysqli if you're only ever going to connect to a MySQL source.