一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php 输出xml 文档实例

时间:2022-11-14 23:01:52 编辑:袖梨 来源:一聚教程网

有时可以是有益的对当前数据库教程模式转储。下面的脚本读取MySQL数据库和输出的XML

描述模式架构。

首先,我们连接到MySQL数据库和使用SHOW TABLES命令返回所有数据库中的表。下一

步,我们遍历每个表和返回每个使用SHOW场命令表中的字段。最后,我们提出了到XML

返回的所有信息。

有一个看一看代码:


// 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教程_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 = " ";
$output .= "";

// iterate over each table and return the fields for each table
foreach ( $tables as $table ) {
$output .= "

";
$result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

while( $row1 = mysql_fetch_row($result_fld) ) {
$output .= " $output .= ($row1[3] == "PRI") ? " primary_key="yes" />" : " />";
}

$output .= "

";
}

$output .= "";

// tell the browser what kind of file is come in
header("Content-type: text/xml");
// print out XML that describes the schema
echo $output;

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

另一方法


$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( $row1 = mysql_fetch_row($result_fld) ) {
$fieldNode = $document->createElement("field");
$tableNode->appendChild($fieldNode);

$fieldNode->setAttribute("name", $row1[0]);
$fieldNode->setAttribute("type", $row1[1]);
if ($row1[3] == "PRI")
$fieldNode->setAttribute("primary_key", "yes");
}
}

...

echo $document->saveXML();

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

php 输出word文档

在此方法中您需要格式化的HTML / PHP页面使用Word友好CSS和标头信息添加到您的

PHP脚本。请确保您不使用因为一切外部样式表应在相同的文件。

因此,用户将被提示下载文件。这个文件将不会被100%的“原始”的Word文档,但它

肯定会在MS Word中打开应用程序。你可以使用这个既用于Unix和Windows环境的方法

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo "";
echo "";
echo "";
echo "My first document";
echo "";
echo "";
?>

方法二

方法2 - 使用COM对象

请注意,在服务器运行下面必须有MS Word中所述的代码安装。 COM将只能在Windows

上工作。

Word文档保存到临时目录,然后送往通过readfile()函数来浏览器

// 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);

热门栏目