Skip to main content
Inspiring
February 4, 2014
Question

PHP - XML - if else

  • February 4, 2014
  • 1 reply
  • 1451 views

Hi,

I hope somebody can help me out with this.

I use this php script to create an XML document for use in AS3.

The connection works and I get the data if I want.

But when the recordset is empty I want the XML to display a message to make clear there are no data this time.

I cannot get this done.

Can somebody point out to me where I go wrong.

Any help is greatly appreciated.

Here is the script I tried several versions of it though:

-------------------------------

<?php

$link = mysql_connect("zzzz", "zzzz", "zzzz");

mysql_select_db("jonettest");

$query = 'SELECT * FROM zzzz WHERE ag_kop >= "a" AND ag_kop <= "b" ORDER BY ag_kop ASC';

$results = mysql_query($query);

$geen = "no program this month";

$geendatum = "-----";

echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";

echo "<dataLoaded>\n";

?>

<?php if (!empty($results)) { ?>

<?php  while ($line = mysql_fetch_assoc($results)) {  ?>

<?php

          echo "<verhaal>\n";

          echo "<id>" . $line["ag_id"] . "</id>\n";

          echo "<datum><![CDATA[" . $line["ag_datum"] . "]]></datum>\n";

          echo "<kop><![CDATA[" . $line["ag_kop"] . "]]></kop>\n";

          echo "<tekst><![CDATA[<textformat leading='6'>" . $line["ag_tekst"] . "</textformat>]]></tekst>\n";

          echo "<info><![CDATA[<textformat leading='6'>" . $line["ag_info"] . "</textformat>]]></info>\n";

          echo "</verhaal>\n";

?>

<?php }  ?>

<?php }  else { ?>

<?php

          echo "<verhaal>\n";

          echo "<id>" . 2 . "</id>\n";

          echo "<datum><![CDATA[" . $geendatum . "]]></datum>\n";

          echo "<kop><![CDATA[" . $geen . "]]></kop>\n";

          echo "<tekst><![CDATA[<textformat leading='6'>" . $geen . "</textformat>]]></tekst>\n";

          echo "<info><![CDATA[<textformat leading='6'>" . $geen . "</textformat>]]></info>\n";

          echo "</verhaal>\n";

?>

<?php } ?>

<?php

echo "</dataLoaded>\n";

mysql_close($link);

?>

This topic has been closed for replies.

1 reply

BazBat
Participating Frequently
February 4, 2014

Your code should work however check your SQL

'SELECT * FROM zzzz WHERE ag_kop >= "a" AND ag_kop <= "b" ORDER BY ag_kop ASC';

you shouldn't use >= on a string just use =

if you run the following does it work:

<?php

$link = mysql_connect("zzzz", "zzzz", "zzzz");

mysql_select_db("jonettest");

$query = 'SELECT * FROM zzzz WHERE ag_kop = "a" AND ag_kop = "b" ORDER BY ag_kop ASC';

$results = mysql_query($query);

if ( empty($results) ) {

echo 'empty';

}

?>

Inspiring
February 4, 2014

Doesnot work if the recordset is empty:

if (empty($results) ) {

echo 'empty';

}

?>

Does work if the recordset is filled :

if (!empty($results) ) {

echo 'OK';

}

?>

What could be wrong?

BazBat
Participating Frequently
February 4, 2014

Thinking about it empty can return true if its an empty string. Use:

if($results > 0) {

in place of:

if (!empty($results) ) {