Jump to content

Selecting all mysql tables in PHP and outputing data to xml


joeandrews91

Recommended Posts

I have the following code, and really can't see what is wrong with it. Any help would be great.

 

<?php

if (!mysql_connect('127.0.0.1', 'root', '')) {

echo 'Could not connect to mysql';

exit;

}

 

$dbname = 'Requests';

$result = mysql_list_tables($dbname);

if (!$result) {

echo "DB Error, could not list tables\n";

echo 'MySQL Error: ' . mysql_error();

exit;

}

 

while($row2 = mysql_fetch_row($result)) {

foreach ($row2[0] as $table_id) {

 

$query = "SELECT * FROM $table_id";

$dbresult = mysql_query($query);

 

// added code to use the tablename and select all records from that table

// create a new XML document

$doc = new DomDocument('1.0');

// create root node

$root = $doc->createElement('mixes');

$root = $doc->appendChild($root);

// process one row at a time

while($row = mysql_fetch_assoc($dbresult)) {

  // add node for each row

  $occ = $doc->createElement($table_id);

  $occ = $root->appendChild($occ);

  // add a child node for each field

  foreach ($row as $fieldname => $fieldvalue) {

    $child = $doc->createElement($fieldname);

    $child = $occ->appendChild($child);

$value = $doc->createTextNode($fieldvalue);

    $value = $child->appendChild($value);

  } // foreach

} // while

}

}

 

 

echo 'Wrote: ' . $doc->save("s.xml") . ' bytes'; // Wrote: 72 bytes

 

?>

Link to comment
Share on other sites

Hi,

 

Thanks for the reply, the first foreach loop doesn't work. I am unsure if you can select from all tables in the database. If i just choose one table to be printed to xml everything works fine.

However, i need to print the data from every table to xml, and can't see how to do this. Will this work with a foreach loop.

 

I am quite new to this, so apologies if I am confusing, I'm confusing myself.

 

The code below is what i have problems with.

 

$table_id = array();

while($rows = mysql_fetch_array($result, MYSQL_NUM)) {

if (!$rows) {

 

    echo 'MySQL Error: ' . mysql_error();

    exit;

}

 

 

}

foreach ($rows[0] as $table_id){

$query = "SELECT * FROM $table_id";

$dbresult = mysql_query($query);

 

Link to comment
Share on other sites

It is still not clear..

 

But  I think there is issue here

 

foreach ($row2[0] as $table_id) {  

 

Becauese $row2[0] is not an array but the table name. So you dont need a foreach to get its value

 

Just the statment 

$table_id = $row2[0];

will do .

 

so replace 

foreach ($row2[0] as $table_id) {  loop

  with statement

$table_id = $row2[0];

 

and try...

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.