0

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.....                
     }
   }
}
user3702602
  • 139
  • 3
  • 16
  • What is your question **exactly**? – u_mulder Jun 27 '14 at 07:30
  • If you don't find a result, you won't get to your `if(empty($empty_config))` test. Check for the number of rows. Check the [documentation](http://php.net/mysql_num_rows) for details and be sure to read that deprecation warning... the days for your code to work at all are numbered. –  Jun 27 '14 at 07:31
  • it looks completed what i have written but its simple. If in the dashboard_widget_users table as shown the configuration is empty then the configuration from dashboard_widget table should show... – user3702602 Jun 27 '14 at 07:32
  • please check the updated question..now it looks simple.. i have remove further part....the condition for !empty($empty_config) is getting satisfied but the condition empty($empty_config) its not getting satisfied...i am making mistake in my if else condition i guess... – user3702602 Jun 27 '14 at 07:37

1 Answers1

1

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.

Community
  • 1
  • 1
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59