简体   繁体   中英

if else condition is not run properlly

This is my code and I am unable to run my simple condition. I have used all the ways but it's not working. As I wanted that if count is available then show count and if not then echo 0 in . I know it is simple but I am stuck on it.

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$db = "usman";

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
$result = $dbh->query(
    "SELECT COUNT(*) as cnt
        FROM ams
        where empid= {$_SESSION['sess_user_id']}
        GROUP BY leavetype
        HAVING leavetype = 'Annual'"
);

if (!$result) {
    echo "<td>";
    echo 0;
    echo "</td>";
} else {
    foreach ($result as $row) {
        echo "<td>" . $row['cnt'] . "</td>";
        echo "<br>";
    }
}
?>

Your solution

<?php
session_start();

$hostname = "localhost";  
$username = "root";  
$password = "";  
$db = "usman";  

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  
$result = $dbh->query("SELECT COUNT(*) as cnt FROM ams where empid= {$_SESSION['sess_user_id']} GROUP BY leavetype HAVING leavetype = 'Annual'");

if (!$result) {
    echo "<td>" . 0 . "</td>";
} else {
    foreach($result as $row) {
        echo "<td>" . $row['cnt'] . "</td>";  
        echo "<br>";
    }
}
?>

Explanation

It appears you are missing a } at the end of your if-else loop. Additionally, if you are using $_SESSION super-global variables, you need to invoke session_start() first.

Note: While using if else conditions from the Query it is advisable to echo the Query and exit the operations after that line so that we can see the query that we have to execute in the browser and you can navigate to phpmyadmin and place the query in SQL and click on GO . So that this is one of the very easy method to find whether the query runs or not.

Missing Condions:

  1. Session Start is missing in the file
  2. Close brace for the loops opened is missing

General Instruction: Sometimes if the query is not executing properly also the result of the query will return FALSE so that it is adviced to take the count of the rows after tht query is executed and perform the operations after that.

Your Correct Code look like:

<?php
session_start();
$hostname = "localhost";  
$username = "root";  
$password = "";  
$db = "usman";  
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  
$result = $dbh->query("SELECT COUNT(*) as cnt FROM ams where empid='".$_SESSION['sess_user_id']."' GROUP BY leavetype HAVING leavetype = 'Annual'");
$count = $result->num_rows;
if($count==0)
{
    echo "<td>" . 0 . "</td>";  
}
 else {
    foreach($result as $row) {
        echo "<td>" . $row['cnt'] . "</td>";  
        echo "<br>";
    }
}
?>

If you check the condition with the count of the query executed you can attain your solution in a better way and you can understand very easy so that it avoids confusion if you look at the code some years back too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM