PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kkonline
Forum Contributor
Posts: 251 Joined: Thu Aug 16, 2007 12:54 am
Post
by kkonline » Sun Aug 19, 2007 11:51 pm
I am using the following code to convert data in db and display as xml
Code: Select all
<?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
Forum Contributor
Posts: 251 Joined: Thu Aug 16, 2007 12:54 am
Post
by kkonline » Mon Aug 20, 2007 12:40 am
Hi i figured it out, i was missing a do while loop
The corrected code is
Code: Select all
<?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);
?>