简体   繁体   English

如果其他条件无法验证php mysql中的mysql查询

[英]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. 嗨,我有一个使用if else条件执行查询的小问题。 I have 2 tables ie 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. 我有2个表,即dashboard_widget和dashboard_widget_users,在这两个表中我都存储了我的反序列化配置。用户将始终从dashboard_widget_users表中获取配置。但是如果配置为Null,则它将默认从dashboard_widget表中获取配置。我只想使用if else条件,但我尝试这样做但无法正确执行。if(!empty($ empty_config))的条件已得到满足,但如果为空,则没有任何结果。谢谢。

dashboard_widget_users table dashboard_widget_users表 在此处输入图片说明

dashboard_widget table dashboard_widget表 在此处输入图片说明

Here is my php code: 这是我的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 ($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.....                
     }
   }
}

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. 注意:您应该研究mysqli_*函数,不建议使用mysql_*函数。 Also check this link . 还要检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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