针对android应用程序返回错误响应的PHP API

针对android应用程序返回错误响应的PHP API,php,android,api,Php,Android,Api,我正在为PHP中的android开发人员创建一个API,他希望从数据库中删除一些值,并希望在此之后显示一条消息。 现在的问题是此数据正在成功删除,但此API在完成此过程后始终显示else part消息。如果我删除else部分,它将返回null,这会使android应用程序崩溃。所以我只想给android开发者一个合适的json消息 这是我正在尝试的代码 我知道这个逻辑有问题。但我需要专家建议我的逻辑哪里有错误,我必须做什么才能成功更改代码: if($total_user_count > 0

我正在为
PHP
中的
android
开发人员创建一个
API
,他希望从数据库中删除一些值,并希望在此之后显示一条消息。 现在的问题是此数据正在成功删除,但此API在完成此过程后始终显示else part消息。如果我删除else部分,它将返回
null
,这会使android应用程序崩溃。所以我只想给android开发者一个合适的
json
消息

这是我正在尝试的代码

我知道这个逻辑有问题。但我需要专家建议我的逻辑哪里有错误,我必须做什么才能成功更改代码:

if($total_user_count > 0)
                {

                    $tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
                    foreach($tables_data as $table_data) 
                    {
                        $user_sql = mysql_query("delete from $table_data where user_name='$user_name'");

                        if($user_sql)
                        {
                            $response['success'] = 1;
                            $response['user']['error_msg'] = 'Clear Successfully All History!';             
                        }
                    }
                }
                else
                {

                    $response['success'] = 0;
                    $response['user']['error_msg'] = 'Record Not Found!';
                }
对这个

if($total_user_count > 0)
                {

                    $tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
                    foreach($tables_data as $table_data) 
                    {
                        $user_sql = mysql_query("delete from $table_data where user_name='$user_name'");

                    }
                    $response['success'] = 1;
                    $response['user']['error_msg'] = 'Clear Successfully All History!';
                }

原始代码的问题在于,您正在循环中设置成功/失败。四个表中的一个可能/可能不包含用户名。如果最后一个表没有,那么根据您的逻辑,即使循环的上一次迭代删除了username所在表中的数据,您也会得到“recordnotfound”

<?php
    $conn = mysqli_connect(.....);
     if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes"  && $user_name_id == $user_name_id1) {               
                $tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
                $userHistoryDeleted = 0;
                foreach($tables_count as $table_count) {
                     //if history is found, then it will be deleted otherwise not
                     mysql_query("delete from $table_count where user_name = '$user_name'");
                     if(mysqli_affected_rows($conn)) {
                          $userHistoryDeleted = 1;
                     }

                }
                $msg = 'Record Not Found!';
                if($userHistoryDeleted) {
                     $msg = 'Clear Successfully All History!';
                }
                $response['success'] = $userHistoryDeleted;
                $response['user']['error_msg'] = $msg;
    }
试试这个:如果($total_user_count>0){$tables_data=array(“property_for_sale”、“property_for_rent”、“cpo_post_requirements”);foreach($tables_data as$table_data){$user_sql=mysql\u query(“从$table_data中删除,其中user_name='$user_name'”;}$response['success']=1;$response['user']['error_msg']='Clear Successfully All History!';}并删除其他部分。”
<?php
    $conn = mysqli_connect(.....);
     if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes"  && $user_name_id == $user_name_id1) {               
                $tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
                $userHistoryDeleted = 0;
                foreach($tables_count as $table_count) {
                     //if history is found, then it will be deleted otherwise not
                     mysql_query("delete from $table_count where user_name = '$user_name'");
                     if(mysqli_affected_rows($conn)) {
                          $userHistoryDeleted = 1;
                     }

                }
                $msg = 'Record Not Found!';
                if($userHistoryDeleted) {
                     $msg = 'Clear Successfully All History!';
                }
                $response['success'] = $userHistoryDeleted;
                $response['user']['error_msg'] = $msg;
    }