aktiwers
September 26th, 2011, 02:21 AM
Could someone help me out with this.
I have a database table called wp_users. In this table there is a row called user_login, which contains all the usernames.
Now I want to search through these usernames, to check if they exist or not.
I do it this way and it seams to work:
// db connection
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
//Get usernames from db to $usernames
$usernames = array();
$results = mysql_query("SELECT * FROM wp_users");
while ($row = mysql_fetch_array($results))
{
$usernames[] = $row['user_login'];
}
{
if (in_array($name, $usernames, true))
{
$response="Username not in use!";
}
else
{
$response="Username in use!";
}
}The thing I dont understand is - when my "if (in_array($name, $usernames, true))" finds a match it prints the ELSE statement, and when it does not find a match it prints the IF statement.
Shouldn't it be the other way around? When "if (in_array($name, $usernames, true))" is true, it prints the first statement, and when false the last statement (the ELSE statement).
Or could someone explain this to me?
I have a database table called wp_users. In this table there is a row called user_login, which contains all the usernames.
Now I want to search through these usernames, to check if they exist or not.
I do it this way and it seams to work:
// db connection
$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
//Get usernames from db to $usernames
$usernames = array();
$results = mysql_query("SELECT * FROM wp_users");
while ($row = mysql_fetch_array($results))
{
$usernames[] = $row['user_login'];
}
{
if (in_array($name, $usernames, true))
{
$response="Username not in use!";
}
else
{
$response="Username in use!";
}
}The thing I dont understand is - when my "if (in_array($name, $usernames, true))" finds a match it prints the ELSE statement, and when it does not find a match it prints the IF statement.
Shouldn't it be the other way around? When "if (in_array($name, $usernames, true))" is true, it prints the first statement, and when false the last statement (the ELSE statement).
Or could someone explain this to me?