if else statment w/ php & mysql

liunx

Guest
plz help.. whenever i try to fix it... like change "!="
to "=" it goes from one extreme to another... it says "Username taken!" all the time if i use "!="... it says "Done!" all the time if i use "==".........


$query = mysql_query("SELECT username FROM members WHERE username='$username'");

if ($username !== username) {

echo "Username taken";

}

else {

mysql_query("INSERT INTO members(username,password,email) VALUES('$username','$password','$email')");

echo "Done";

}!== is not valid.


$query = mysql_query("SELECT COUNT(*) as count FROM members WHERE username='$username'");

$results = mysql_query($query, $link);

while($row = mysql_fetch_array($results)) {

if ($row["count"] > 0) {

echo "Username taken";

}

else {

mysql_query("INSERT INTO members(username,password,email) VALUES('$username','$password','$email')", $link);

echo "Done";

}
}


I suggest you try that. However, it's a rather simple way of comparing usernames.. You should rather make it check for a few more things. dont forget to addslashes() for security..Originally posted by jeremy
plz help.. whenever i try to fix it... like change "!="
to "=" it goes from one extreme to another... it says "Username taken!" all the time if i use "!="... it says "Done!" all the time if i use "==".........


$query = mysql_query("SELECT username FROM members WHERE username='$username'");

if ($username !== username) {

echo "Username taken";

}

else {

mysql_query("INSERT INTO members(username,password,email) VALUES('$username','$password','$email')");

echo "Done";

}


'!=' - Does not equal.
'==' - is equal to
'=' - equalsWe have a PHP forum now for PHP questions. Please post PHP questions in that forum.Originally posted by Rydberg
!== is not valid.


$query = mysql_query("SELECT COUNT(*) as count FROM members WHERE username='$username'");

$results = mysql_query($query, $link);

while($row = mysql_fetch_array($results)) {

if ($row["count"] > 0) {

echo "Username taken";

}

else {

mysql_query("INSERT INTO members(username,password,email) VALUES('$username','$password','$email')", $link);

echo "Done";

}
}


I suggest you try that. However, it's a rather simple way of comparing usernames.. You should rather make it check for a few more things. dont forget to addslashes() for security..
I wouldn't even do that. this is more simplier

$query = mysql_query("SELECT COUNT(*) as count FROM members WHERE username='$username'");

if (mysql_num_rows($query) > 0) {
echo "Username taken";
} else {

mysql_query("INSERT INTO members(username,password,email) VALUES('$username','$password','$email')", $link);

echo "Done";

}
}

and Jeremy,

this

if ($username !== username) {

is invalid all the way. for 1 they already told you that !== doesn't exist, but did they tell you that username is a string and NOT a variable? beside you didn't even get the $username from the database so that first query is doing you no good.Well I apologize for my idiotic mistake. I'll try to keep it more simplier next time.I'm sorry, I'm having a bad day.Originally posted by Jokerman
'=' - equals
actually.. using '=' e.g.

if ( $blah=blah ) {
}

makes $blah a variable so its basically asking if variable is true .. do thisno, he had it right.

what you want Jolly is the double = sign for that.

$blah = blah; // set the variable
$blah == blah // if ture (is equal to)
$blah != blah // does not equal to

in a if statement you have to use ==I don't think that's correct. It works in a while-loop at least. Quoted from above:

while($row = mysql_fetch_array($results))

I can only imagine that similar statements are valid also in an 'if', as the only difference between 'while' and 'if', is that the 'while' is looped until the statement is false, i.e. that the variable is given a 'false' value such as null.

:hug:no that is a lot different.

$a == $b TRUE if $a is equal to $b.

when you do it in a if statements then you are making a comparison. a while statment is different, especially like you showed.

the while loop is doing exaclty what you said, looping, the if statment is not.I don't really see the difference.. A while loop is also a comparison, right? If the statement is true then it will work. If the variable to the left gets a true value, it will work.

if($a == $b)
if $a is equal to $b, do something

while($a == $b)
if $a is equal to $b, do something, and do it until $a is not equal to $b.

After testing, I have found out that this works:


<?

$b = true;

if($a = $b)
echo "Yes sir!<br>";

while($c = $b) {
echo "Indeed!";
$b = false;
}

?>


I draw the conclusion that if($a = $b) is a valid if statement. You seem certain that you're right though, so maybe we're just misunderstanding each other.if($a = $b)

is not the same as this

if($a == $b)

so if you want the variable to be true than you have to use ==

so take this for example.


<?
$a=2;
$b=1;
if($a == $b){
echo "it is true";
} else{
echo"it is false";
}

if($a = $b){
echo "<br>it is true";
} else{
echo"<br>it is false";
}

?>


you will see that the first if statement is false as teh second one is true. why you say becasue the second one is assigning $a as 1 and it is telling you that it was assigned ok.

so yes they are different.Well it is clear to me now that we're misunderstanding each other, or you're just trying to escape the fact that you were wrong. I don't think I would get very far in programming, if I didn't know how to use simple if statements.

I quote you scoutt, you corrected jollyfactory with "in a if statement you have to use ==". I am just trying to show you that this is wrong, that '=' is valid in some cases, just like jollyfactory first mentioned.let me retract what I said.
in a if statement you have to use ==
yes you can use the if($a=$b) in certain situations, but this is not what jeremy wants. that is all I meant. I didn't clarify that, I was a little vague.

I will admit when I am wrong if I was. in this case of the thread the if($a=$b) is WAY different than what he wants.

$a=$b is assigning the variable, it is not comparing it.

in this sutuation

if ($username !== username) {

you cannot use the if($a=$b) method. that is all I was saying.

why you would want to use if($a=$b) is beyond me as all it is doing is assigning the variable a to whatver variable b is.Well then it was just a misunderstanding as I suspected. That $a=$b won't replace what he meant with $a!==b is obvious, I thought that was implied since I had corrected it in my first post(although I changed the statement a bit ;))... I don't really know what I'd use $a=$b for in PHP either... I think it can be more useful in programming languages such as C++, if I remember correctly, where floating data types aren't used.. Well, I don't remember much of that anyway. I'm glad we got this sorted then, what a great way to spend my 100th post :D

:cheers:lol.. i knew = didnt work.. lol. heres my code thats EVEN more simpiler :):

$query = mysql_query("SELECT username FROM members WHERE username = '" . $username . "'");
if($query && mysql_num_rows($query) > 0)
{
echo "Username already taken";
}

else
{

mysql_query("INSERT INTO members(username,password,email) VALUES('$username','$password','$email')");

echo "Successful! Now please <a class='content' href='http://www.htmlforums.com/archive/index.php/index.php'>login</a> :) ";
 
Back
Top