Create Variable from MySQL Cell Inside Loop PHP -
i hope title sufficient contents of question...
i've patched little script various sources writes every row in mysql database separate xml file. supposed do, i'm hung on naming convention.
i'd name xml file 'id' column of row being exported, $id
is name of file sans extension.
tried accessing through xpath no avail.
how access particular column's data (cell) , assign variable?
<?php $host = ""; $user = ""; $pass = ""; $database = ""; // replace query matches database $sql_query = "select * data"; $db_link = mysql_connect($host, $user, $pass) or die("could not connect host."); mysql_select_db($database, $db_link) or die ("could not find or access database."); $result = mysql_query ($sql_query, $db_link) or die ("data not found. sql query didn't work... "); //rows while ($row = mysql_fetch_array($result, mysql_assoc)) { $id; $xml = "<?xml version=\"1.0\"?>\n"; $xml .= "<order>\n"; $xml .= "\t<item>\n"; $i = 0; // cells foreach ($row $cell) { // escaping illegal characters - not tested ;) $cell = str_replace("&", "&", $cell); $cell = str_replace("<", "<", $cell); $cell = str_replace(">", ">", $cell); $cell = str_replace("\"", """, $cell); $col_name = mysql_field_name($result,$i); // creates "<tag>contents</tag>" representing column $xml .= "\t\t<" . $col_name . ">" . $cell . "</" . $col_name . ">\n"; $i++; } $xml .= "\t</item>\n"; $xml .= "</order>\n"; $handle = fopen($id.'.xml','w+'); fwrite($handle,$xml); fclose($handle); } ?>
i managed spend hours on google , rewrote using xmlwriter:
<?php $host = ""; $user = ""; $pass = ""; $database = ""; mysql_connect($host, $user, $pass) or die ("could not connect host."); mysql_select_db($database) or die("could not find or access database"); $sql = "select * data"; $res = mysql_query($sql) or die ("data not found. sql query didn't work..."); //start document while ($row = mysql_fetch_assoc($res)) { //use variable filename later $name = $row['id']; $xml = new xmlwriter(); $xml->openmemory(); $xml->startdocument('1.0', 'utf-8'); $xml->setindent(true); $xml->startelement('order'); $xml->startelement('item'); foreach($row $key => $value) { $xml->writeelement($key,$value); } //$xml->writeelement('id',$row['id']); $xml->endelement(); $xml->endelement(); //close document $xml->enddocument(); file_put_contents($name .'.xml', $xml->outputmemory()); } ?>
Comments
Post a Comment