1

I am working in an Android application that is using an Apache server to recover information from a MySQL database with PHP. I do not know the proper way to manage situations where the request to the database is returning 0 records. In this case I want to minimize as much as possible the process. So far I can only figure out two ways: 1.- Return an specific XML format and then it is managed by the client. So when this find a specific tags knows there is not records in the database. I do not like this solution because I would like to catch the issue before I parse the XML. The PHP code for this would be something like this.

<?php
include ("common_function.php");
include("generate_DOM_XML.php");

//Make database connection
connectDB(); 

//set el cotejamiento
mysqli_query($mysqli,"SET NAME 'utf8'");

//sentence sql
$list_news_sql="SELECT new_title, new_body, new_create_time FROM table_news ORDER BY new_create_time DESC";
//results sql query
$list_news_sql_result = mysqli_query($mysqli, $list_news_sql) or die (mysqli_error($mysqli));

if(mysqli_num_rows($list_news_sql_result) < 1){
$message = "<empty>No records</empty>;
echo $message;

}else{
    //First parmenter MUST be send AFTER the request is done
    $xml_output = mysql_XML($mysqli,$list_news_sql_result,"news","new");
    mysqli_free_result($list_news_sql_result);
    closeDB();
        echo $xml_output;
    }
?>

2- Force the server to send an error message and ctach up it before I parse the xml. To make it I do not know if it is possible to use exit(400) or something like that and the capture the message in the client with HttpUrlConnection.getResponseCode() using a sentence if()....else and take a decission before I start the parsing process

Shibu Thomas
  • 3,148
  • 2
  • 24
  • 26
  • 1
    You could have a look at my [answer](http://stackoverflow.com/questions/21392909/multiple-httpurlconnection-calls-for-get-throwing-premature-end-of-file-exceptio/21399110#21399110). As this is not a client error (4xx), I suggest using HTTP code `204 No Content`. – hgoebl Jan 31 '14 at 10:07
  • @hgoebl I have changed the php based in your comment, so it is returning `header("http/1.1 204 No content"); echo "no data";` I tink it will work for me. So thanks –  Jan 31 '14 at 12:35

0 Answers0