How to check if the result of select mysql expression has a value or not in php?

I have a textbox button UserName and Check Availability next to it ..... I checked availability by passing the value UserName textbox ..... But this does not seem to work ....

That's what I'm doing?

 echo $UserName = $_GET['CheckUsername']; $_SESSION['state'] = $State; $queryres = "SELECT dUser_name FROM tbl_login WHERE dUser_name='$UserName'"; $result = mysql_query($queryres,$cn) or die("Selection Query Failed !!!"); if($result==true) // this condition doesn't seem to work { echo "User Name Available"; } else { echo "Sorry user name taken"; } 
+4
source share
6 answers

Please make sure you avoid your MySQL entries. Transferring data directly from $_GET , $_POST or any other superglobal is unsafe.

 // Escape any quote characters in the input $UserName = mysql_real_escape_string($_GET['CheckUsername'], $cn); $_SESSION['state'] = $State; $queryres = "SELECT dUser_name FROM tbl_login WHERE dUser_name='$UserName' LIMIT 1"; $result = mysql_query($queryres, $cn) or die("Selection Query Failed !!!"); if (mysql_num_rows($result) > 0) { echo 'User name exists in the table.'; } else { echo 'User name does not exist in the table.'; } 
+15
source
 if ($result == false) { echo "not available"; } else { echo "available"; } 

The reason your work is not working is because the value of $ result will not be true, since it contains an array. You can do this by modifying the statement to check false first.

You can also do this with mysql_num_rows

 if (mysql_num_rows($queryres) > 0) { echo "User name taken"; } 
+4
source

from http://php.net/mysql_query :

Return values

For SELECT, SHOW, DESCRIBE, EXPLAIN, and other statements that return a result set, mysql_query () returns the resource with success or FALSE on error.

So, to start with, the if statement will never evaluate to false because you have "or die" in mysql_query (which is activated when mysql_query returns false).

But your if condition is not at all because you do not want to know if the query has completed, but if any results were returned. For this, you probably want to use mysql_num_rows.

As a small tip, since you only need to know if there is 1 matching username, add "LIMIT 1" at the end of your query. Then mysql will return as soon as it hits the first match, instead of searching the whole table for more results.

As an alternative method, you can use the COUNT () query and check the number returned in the result set.

+1
source

Although this question already has an adequate answer, I will give you another option. Just remove "== true" in the IF condition, and it will automatically check for a value instead of a Boolean comparison.

 if($result) // this condition will now work { echo "User Name Available"; } else { echo "Sorry user name taken"; } 

You can use this if you want to keep the signature of your code.

+1
source

After executing the query, simply mysql_num_rows ($ result), which returns you, select the number of rows.

+1
source
 $queryres = "SELECT dUser_name FROM tbl_login WHERE dUser_name='$UserName'"; $result = mysql_query($queryres,$cn) or die("Selection Query Failed !!!"); if (mysql_num_rows($result)) { echo "Username available"; } else { echo "Username not available"; } 
+1
source

Source: https://habr.com/ru/post/1309184/


All Articles