user3702602
user3702602

Reputation: 139

if else condition not validating for mysql query in php mysql

Hi i have a small problem with the execution of query using if else condition. I have 2 tables i.e dashboard_widget and dashboard_widget_users and in both this table i store my unserialize configuration.Users will always get the configuration from dashboard_widget_users table.But if the configuration is Null then it will take bydefault configuration from dashboard_widget table.I just want to use if else condition and i tried to do so but unable to execute it properly.The condition for if(!empty($empty_config)) is getting satisfied but if it empty then i am not getting any result.Thank you.

dashboard_widget_users table enter image description here

dashboard_widget table enter image description here

Here is my php code:

$query = "SELECT dashboard_widget_users.configuration
    FROM dashboard_widget_users
    INNER JOIN yw_user ON dashboard_widget_users.dsnr_yw_user = yw_user.intern
    INNER JOIN dashboard_widget ON dashboard_widget_users.dsnr_dashboard_widget = dashboard_widget.id
    WHERE dashboard_widget_users.dsnr_yw_user =10 AND dashboard_widget.id =1 ";  
$result = mysql_query($query, $myConnection);
if ($row = mysql_fetch_assoc($result)) 
{
    $empty_config=$row['configuration'];
    if (empty($empty_config)) {
        $sql="SELECT dashboard_widget.configuration FROM dashboard_widget WHERE Id =1";
        $sql_result = mysql_query($sql, $myConnection);
        $results = mysql_fetch_assoc($sql_result);  
        $config= unserialize($results['configuration']);
    }
    if (!empty($empty_config)) {
        $config = unserialize($row['configuration']);

        foreach($config as $val)
        {
            //Here is my further things.....                
     }
   }
}

Upvotes: 0

Views: 754

Answers (1)

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18113

You should check if there are any rows found, if so, display the information, if not, display the default info:

<?php
$query = "SELECT dashboard_widget_users.configuration
    FROM dashboard_widget_users
    INNER JOIN yw_user ON dashboard_widget_users.dsnr_yw_user = yw_user.intern
    INNER JOIN dashboard_widget ON dashboard_widget_users.dsnr_dashboard_widget = dashboard_widget.id
    WHERE dashboard_widget_users.dsnr_yw_user =10 AND dashboard_widget.id =1 ";  
$result = mysql_query($query, $myConnection);

if( mysql_num_rows( $result ) > 0 ) {
    // row found, display it
}
else {
    // row not found, display default
}
?>

Note: you should look into mysqli_* functions, mysql_* functions are deprecated. Also check this link.

Upvotes: 1

Related Questions