I'm getting a string from a $_GET and I want to test if it could be a boolean, before I use it for a part of a mysql query. Is there a better way of doing it than:
function checkBool($string){
$string = strtolower($string);
if ($string == "true" || $string == "false" ||
$string == "1" || $string == "0"){
return true;
}
else {
return false;
}
}
if (checkBool($_GET['male'])){
$result = mysql_query(
"SELECT * FROM my_table " .
"WHERE male='".$_GET['male']."'") or die(mysql_error());
}
male? The MySQL boolean type is really just a tinyint(1) so if it's that then you should only check for 1s or 0s. If you're using char/varchar and have previously set it to "true" then comparing it with "yes" or "1" will still not match.SELECT * FROM my_table WHERE male='true'. This will actually return rows where male=0 because MySQL tries to convert the STRING "true" to a number but can't so it gives it the value 0. Take out the single quotes and it works as expected.