PHP output XML Document instance

Source: Internet
Author: User
Tags foreach mysql tutorial php script readfile

Sometimes it can be useful to dump the current database tutorial mode. The following script reads the MySQL database and outputs the XML

Describes schema schemas.

First, we connect to the MySQL database and use the show Tables command to return the tables in all the databases. Next

Step, we iterate through each table and return each field using the Show Field command table. Finally, we propose to the XML

All the information returned.

Have a look at the code:


<?php
Database Constants
Make sure the information is correct
Define ("Db_server", "localhost");
Define ("Db_user", "root");
Define ("Db_pass", "password");
Define ("Db_name", "tutorials");

Connection to the database
$dbhandle = MySQL Tutorial _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 examples");

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 "?>";
$output. = "<schema>";

Iterate over each table and return the fields to each table
foreach ($tables as $table) {
$output. = "<table name=" $table ">";
$result _fld = mysql_query ("Show FIELDS from". $table, $dbhandle);

while ($row 1 = mysql_fetch_row ($result _fld)) {
$output. = "<field name=" $row 1[0] "type=" $row 1[1] "";
$output. = ($row 1[3] = = "PRI")? "primary_key=" yes "/>": "/>";
}

$output. = "</table>";
}

$output. = "</schema>";

Tell the browser what kind of the ' file is ' come in
Header ("Content-type:text/xml");
Print out XML that describes the schema
Echo $output;

Close the connection
Mysql_close ($dbhandle);
?>

The other side of the law


$document = new DOMDocument (' 1.0 ');
$schemaNode = $document->createelement ("schema");
$document->appendchild ($schemaNode);

foreach ($tables as $table) {
$tableNode. = $document->createelement ("table");
$schemaNode->appendchild ($tableNode);
$result _fld = mysql_query ("Show FIELDS from". $table, $dbhandle);

while ($row 1 = mysql_fetch_row ($result _fld)) {
$fieldNode = $document->createelement ("field");
$tableNode->appendchild ($fieldNode);

$fieldNode->setattribute ("name", $row 1[0]);
$fieldNode->setattribute ("type", $row 1[1]);
if ($row 1[3] = = "PRI")
$fieldNode->setattribute ("Primary_key", "yes");
}
}

...

echo $document->savexml ();

===========================

PHP Output Word Document

In this method you need to format the html/php page using Word friendly CSS and header information to add to your

PHP script. Make sure you do not use because all external style sheets should be in the same file.

Therefore, the user will be prompted to download the file. This file will not be 100% of the "original" Word document, but it

The application will definitely open in MS Word. You can use this method for both UNIX and Windows environments

<?php
Header ("Content-type:application/vnd.ms-word");
Header ("content-disposition:attachment; Filename=document_name.doc ");

echo "echo "<meta http-equiv=" Content-type "content=" text/html;

charset=windows-1252 ">";
echo "<body>";
echo "<b>my-document</b>";
echo "</body>";
echo "?>

Method Two

Method 2-using COM objects

Note that the code described in MS Word must be installed below the server run. COM will only be available in Windows

Work on.

The Word document is saved to the temp directory and sent to the browser via the ReadFile () function

Create New COM Object–word.application
$word = new COM ("Word.Application");

Hide MS Word application window
$word->visible = 0;

Create New Document
$word->documents->add ();

Define page margins
$word->selection->pagesetup->leftmargin = ' 2 ';
$word->selection->pagesetup->rightmargin = ' 2 ';

Define font settings
$word->selection->font->name = ' Arial ';
$word->selection->font->size = 10;

ADD text
$word->selection->typetext ("text!");

Save Document
$filename = Tempnam (Sys_get_temp_dir (), "word");
$word->documents[1]->saveas ($filename);

Close and quit
$word->quit ();
Unset ($word);

Header ("Content-type:application/vnd.ms-word");
Header ("content-disposition:attachment; Filename=document_name.doc ");

Send file to Browser
ReadFile ($filename);
Unlink ($filename);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.