designermonkey Posted May 14, 2010 Share Posted May 14, 2010 I have written the following function to output my database into an xml schema, and I'm having problems with iteration <?php // database constants // make sure the information is correct define("DB_SERVER", "localhost"); define("DB_USER", "root"); define("DB_PASS", "password"); define("DB_NAME", "database"); // connection to the database $dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die("Unable to connect to Server"); mysql_select_db(DB_NAME, $dbhandle) or die("Could not select Database"); // return all available tables $result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle ); // build tables array $tables = array(); while ($row = mysql_fetch_row($result_tbl)) { $tables[] = $row[0]; } $output = "<?xml version=\"1.0\" ?>\n"; $output .= "<database>"; // iterate over each table foreach ( $tables as $table ) { $output .= "\t<table name=\"$table\">"; $fields = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle ); $rows = mysql_query("SELECT * FROM ". $table, $dbhandle) or die("Data not found"); for($i = 0; $i < mysql_num_rows($rows); $i++){ $row1 = mysql_fetch_array($rows, MYSQL_NUM); $x = 0; $output .= "<row>"; while($data = mysql_fetch_row($fields)){ $output .= "<field name=\"$data[0]\" type=\"$data[1]\""; $output .= ($data[3] == "PRI") ? " primary_key=\"yes\">" : ">"; $output .= $row1[$x]; $output .= "</field>"; $x++; $data = null; } $output .= "</row>"; } $output .= "</table>"; } $output .= "</database>"; // tell the browser what kind of file is come in //header("Content-type: text/html"); // print out XML that describes the schema echo $output; // close the connection mysql_close($dbhandle); ?> The correct number of iterations of $i are outputing <row></row> xml, so I know that the 'for' loop is working. The problem lies with the 'while' loop. It works first time and correctly outputs the first iteration of <field></field> xml, but then doesn't function. I know it will have something to do with the mysql functions, but I can't understand what. Any help would be greatly appreciated, this is quite urgent to me aswell, so serious thumbs up for anyone with a solution! Link to comment https://forums.phpfreaks.com/topic/201804-iteration-not-working/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.