Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP - XML - if else

Participant ,
Feb 04, 2014 Feb 04, 2014

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);

?>

TOPICS
Server side applications
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 04, 2014 Feb 04, 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';

}

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 04, 2014 Feb 04, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 04, 2014 Feb 04, 2014

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

if($results > 0) {

in place of:

if (!empty($results) ) {

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 04, 2014 Feb 04, 2014

I tried

if($results > 0) {

before and it didnot work.

I start thinking your first remark about the SQL might be right.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 05, 2014 Feb 05, 2014
LATEST

I changed the SQL and targeted the id with !empty now it works.

Thanks for your help Dsarchy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines