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.....                
     }
   }
}
4
  • What is your question exactly? Commented Jun 27, 2014 at 7: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 for details and be sure to read that deprecation warning... the days for your code to work at all are numbered. Commented Jun 27, 2014 at 7: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... Commented Jun 27, 2014 at 7: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... Commented Jun 27, 2014 at 7:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

no man the problem is i am getting problem here foreach($config as $val) if i found that the configuration is empty from dashboard_widget_users table ...
What do you get when you do var_dump($row); and var_dump($empty_config);??
thanks man just a very small mistake i did was able to identify :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.