Forum Moderators: coopster

Message Too Old, No Replies

DB to XML (loop Problem)

Displays contents of 1st row only

         

kkonline

5:00 am on Aug 20, 2007 (gmt 0)

10+ Year Member



As advised by forum members I am now using the following code to convert data in db and display as xml


<?php
$hostname_conn = "localhost";
$database_conn = "mysql";
$username_conn = "root";
$password_conn = "";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_conn, $conn);
$query_rsAll = "SELECT `subject` FROM `phpnews_news";
$rsAll = mysql_query($query_rsAll, $conn) or die(mysql_error());

$row_rsAll = mysql_fetch_assoc($rsAll);

$totalRows_rsAll = mysql_num_rows($rsAll);

header('Content-type: text/xml');

header('Pragma: public');

header('Cache-control: private');

header('Expires: -1');

echo '<?xml version="1.0" encoding="utf-8"?><root>' ;

if ($totalRows_rsAll > 0) {

echo '<row>';

foreach ($row_rsAll as $column=>$value) {

echo "<$column><![CDATA[" . $row_rsAll[$column] . "]]></$column>";

// note the period . will join strings together.

}

echo "</row>";

}

while ($row_rsAll = mysql_fetch_assoc($rsAll));

echo '</root>';

mysql_free_result($rsAll);

?>

It displays the subject of only the first row of the database rather than displaying the subjects from all the existing rows. (currently i have around 5 rows in table phpnews_news) It displays only the content of the first row.

I guess there's some problem with the loop.

kkonline

5:43 am on Aug 20, 2007 (gmt 0)

10+ Year Member



Hi i figured it out, i was missing a do while loop ;)

The corrected code is
[php]<?php
$hostname_conn = "localhost";
$database_conn = "mysql";
$username_conn = "root";
$password_conn = "";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_conn, $conn);
$query_rsAll = "SELECT `id` FROM `phpnews_news";
$rsAll = mysql_query($query_rsAll, $conn) or die(mysql_error());

$row_rsAll = mysql_fetch_assoc($rsAll);

$totalRows_rsAll = mysql_num_rows($rsAll);

header('Content-type: text/xml');

header('Pragma: public');

header('Cache-control: private');

header('Expires: -1');

echo '<?xml version="1.0" encoding="utf-8"?><root>' ;

if ($totalRows_rsAll > 0) {
do{ // i missed this
echo '<row>';

foreach ($row_rsAll as $column=>$value) {

echo "<$column><![CDATA[" . $row_rsAll[$column] . "]]></$column>";

// note the period . will join strings together.

}

echo "</row>";

}

while ($row_rsAll = mysql_fetch_assoc($rsAll));
} // i missed this
echo '</root>';

mysql_free_result($rsAll);
?>[/php]

Habtom

5:46 am on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



kkonline, you might be able to get a better response if you can post only the relevant part of the code.

Will sticky you in a minute.

Habtom