Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2004, 02:03 PM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with mysql_fetch_array

Simple question:

Php code:


<?php
$db_name = "abc";
$link = mysql_connect("abc", "abc", "abc) or die("Could not connect to server!");
$table_name = 'journals';

$select_db = mysql_select_db($db_name, $link);
$query = "SELECT * FROM " . $table_name;

$result = mysql_query($query, $link) or die("Could not complete database query");
$num = mysql_num_rows($result);

if ($num != 0) {
$file= fopen("results.xml" , "w");
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<$table_name>\r\n";

$row = mysql_fetch_array($result);

while (list($key,$value) = each ($row)){

$_xml .=" <$key>$value</$key>\r\n";

}



$_xml .="</$table_name>";


fwrite($file, $_xml);
fclose($file);
echo $_xml;

}
else {
echo "No Records found";
}

?>

Output:


<?xml version="1.0" encoding="UTF-8" ?>
<journals>
<0>1</0>
<journal_id>1</journal_id>
<1>newsweek</1>
<journal_path>newsweek</journal_path>
<2>Newsweek</2>
<journal_title>Newsweek</journal_title>
<3>The Weekly Magazine</3>
<journal_subtitle>The Weekly Magazine</journal_subtitle>
</journals>

Desired Output:
<?xml version="1.0" encoding="UTF-8" ?>
<journals>
<journal_id>1</journal_id>
<journal_path>newsweek</journal_path>
<journal_title>Newsweek</journal_title>
<journal_subtitle>The Weekly Magazine</journal_subtitle>
</journals>

I know, I could be using $row[journal_id],$row[journal_path] and so on, but the point is, I wanted to make this as modular as possible.
What am I doing wrong...?
Any assistance will be greatly appreciated.
Thank you!





Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql_fetch_array and if .. else Jeff Smitherman Beginning PHP 3 November 21st, 2005 09:08 AM
chapter 15 cart.php Warning: mysql_fetch_array(): pink BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 July 19th, 2005 07:49 PM
mysql_fetch_array with a variable string aeres PHP How-To 7 August 18th, 2003 01:01 PM
mysql_fetch_array problem... aeres PHP Databases 2 July 4th, 2003 12:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.