Help Plz :(

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
wolf7
Forum Newbie
Posts: 1
Joined: Mon Aug 26, 2013 4:59 pm

Help Plz :(

Post by wolf7 »

Hello Sir.
I am trying to convert the MYSQL data into XML using the following code :

Code: Select all

<?php
// database constants
// make sure the information is correct
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "walid");
// connection to the database 
$dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS) 
or die("Unable to connect to MySQL"); 

// select a database to work with 
$selected = mysql_select_db(DB_NAME, $dbhandle) 
or die("Could not select the database"); 

// return all available tables 
$result_tbl = mysql_query( "SHOW TABLES FROM ".DB_NAME, $dbhandle ); 

$tables = array(); 
while ($row = mysql_fetch_row($result_tbl)) { 
$tables[] = $row[0]; 
} 

$output = "<?xml version=\"1.0\" ?>\n"; 
$output .="<tables>\n";
// iterate over each table and return the fields for each table
foreach ( $tables as $table ) { 
$output .= "<table name=\"$table\">\n"; 
$result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle ); 

while( $row1 = mysql_fetch_row($result_fld) ) {
$output .= "<field name=\"$row1[0]\" type=\"$row1[1]\"";
$output .= ($row1[3] == "PRI") ? " primary_key=\"yes\" />\n" : " />\n";


}
$output .= "</table>\n"; 
}
$output .="</tables>\n";

// tell the browser what kind of file is come in
header("Content-type: xml"); 

echo $output;

$handle = fopen("MyXML.xml", "w") or die("unable to create file");
file_put_contents("MyXML.xml", $output);
fclose($handle);


// close the connection 
mysql_close($dbhandle); 
?>



Sir , I have 2 questions :

how can i update the code in order to show the content of the data in MYSQL ? This code is only showing the table and fields name and the fields types.

How i can update the code in order to show the foreign key and relation between tables ?
It is Showing only the primary key .

Help Please.
Thank you
Last edited by requinix on Mon Aug 26, 2013 7:05 pm, edited 1 time in total.
Reason: use [syntax=php] tags for PHP code
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help Plz :(

Post by requinix »

"Help Plz :(" is not a helpful title. I know that you can do better.

What XML format are you trying to output to? What you have now doesn't really work if you want to include the data too.
As for the relationships, what does SHOW FIELDS FROM a table output for an example table?
Post Reply