PHP - XML - if else
Copy link to clipboard
Copied
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);
?>
Copy link to clipboard
Copied
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';
}
?>
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Thinking about it empty can return true if its an empty string. Use:
if($results > 0) {
in place of:
if (!empty($results) ) {
Copy link to clipboard
Copied
I tried
if($results > 0) {
before and it didnot work.
I start thinking your first remark about the SQL might be right.
Copy link to clipboard
Copied
I changed the SQL and targeted the id with !empty now it works.
Thanks for your help Dsarchy

