MariaDB: IF-THEN-ELSE Statement in PHP

84 views Asked by At

Anyone who can help on how to apply MariaDB: IF-THEN-ELSE Statement in PHP.

I want to query when the account type is Admin and he/she created the data in pre_registered table the Cancelled status also display in the list but if he/she is Admin but not the on who created the data it will not display on the list. If normal user is logged in the system, the Cancelled status will not be included in the list.

I have try the below query but did not work.

<?php
     $account_type = getActiveAccountType($connect);

     $query = "SELECT * FROM pre_registered WHERE ";

    if ($account_type == 'Admin') {
        $query .= "
        
        IF registered_created_by != ".$_SESSION['account_id']." THEN
        {
            registered_status != 'Cancelled'
        }
        END IF;
        
        ";
    } else if ($account_type == 'User') {
        $query .= "
            registered_status != 'Cancelled'
        ";
    }

2

There are 2 answers

3
Mhiko Leeps On

I am new to programming as well, but I hope this helps.

Try getting the results first and write a condition that checks if registered_created_by == $_SESSION['account_id'], then show the column if it does and hide if it doesnt.

0
danblack On

You want registered_status != 'Cancelled' in regardless of the user type, so include this in $query.

For the admin user, you want to include results with registered_created_by = $account_id. So in this PHP if branch. Because its an OR condition, it will be included regardless of the registered_status.

$query .= ' OR registered_created_by = ?'

When executing this query as a prepared query, pass $_SESSION['account_id'] as the value to be used in the query.