Hello i am trying to make a simple if else php statement this is my code but isnt working
$sql = "SELECT UserType from user where Username = '$email'";
$result = mysqli_query($connection, $sql);
if($sql == 'A'){
echo 'Passed';
}else{
echo 'Error';
}
?>
i also tried changin the if($sql == 'A')
to if($result == 'A')
but still nothing. Am i missing something?
CodePudding user response:
This might help:
$sql = "SELECT UserType from user where Username = '$email'";
if($result = mysqli_query($connection, $sql)){
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if($row['UserType'] == 'A') {
echo 'Passed';
}
else {
echo 'Error';
}
}
CodePudding user response:
$sql = "SELECT UserType from user where Username = '$email'";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0) {
while($usertype = mysqli_fetch_assoc($result)) {
echo $usertype['UserType'];
if($usertype['UserType'] == 'A'){
//te da
}else{
echo 'False';
}
CodePudding user response:
Actually, you can't compare the query to anything, because, It's not going to return a string, it returns a mysqli_result object
, that you'll have to loop through as an associative array.
In order to do so, I guess you will have to do something like this:
$query = "SELECT UserType from user where Username = '$email'";
$result = mysqli_query($connection, $query);
while ($data = mysqli_fetch_assoc($result)) {
$user = $data['user'];
if ($user == 'A') {
echo 'passed';
} else {
echo 'failed';
}
}
But, if your goal was to test if the connection was successful, you'll simply pass the connection variable to the condition:
if (!$connection) {
die("failed: " . mysqli_connect_error());
} else {
echo "Success!";
}
I wish that's going to help you, or inspired you with some solution.
Good Luck :)