Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP XML解析问题_Php_Sql_Xml - Fatal编程技术网

PHP XML解析问题

PHP XML解析问题,php,sql,xml,Php,Sql,Xml,我希望能够用PHP解析SQL数据库以输出XML,但是我无法让它显示表名及其所有内容。我需要一些帮助,这是我的代码,没有登录信息: 我已经定义了db服务器、mdb用户和密码、db名称;还应该定义什么吗 <?php // connection to the database $dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die("Unable to connect to MySQL"); // select a da

我希望能够用PHP解析SQL数据库以输出XML,但是我无法让它显示表名及其所有内容。我需要一些帮助,这是我的代码,没有登录信息: 我已经定义了db服务器、mdb用户和密码、db名称;还应该定义什么吗

<?php
// 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 data");

// 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 .= "<schema>";

// iterate over each table and return the fields for each table
foreach ( $tables as $table ) {
   $output .= "<table name=\"$table\">";
   $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\" />" : " />";
   }

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

$output .= "</schema>"; 

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

我认为最好使用标准的php类XmlWriter来完成这个任务。 看

您的查询中至少有两处输入错误。您很可能希望:

$result_tbl = mysql_query("SHOW TABLES FROM " . DB_NAME, $dbhandle) or die(mysql_error());
and
$result_fld = mysql_query("SHOW FIELDS FROM $table", $dbhandle) or die(mysql_error());

注意第一个上的串联运算符(
),以及添加的
或die(…)
。永远不要假设查询成功。即使查询字符串本身在语法上是正确的,也有太多其他原因导致无法检查错误情况。

我得到了表名,但没有显示任何内容。您知道如何在不声明每一列的情况下输出表中的所有数据吗?有20多个。您可以在代码中使用与$output相同的循环,但不使用$output。=“我现在将此错误作为输出:您的SQL语法中有一个错误;请查看与您的MySQL服务器版本对应的手册,以了解在第1行的“table name here”附近使用的正确语法生成的查询语句是什么样子的?
$SQL=“。。。"; mysql_query($sql)或die(“Error”.mysql_Error().$sql)
$result_tbl = mysql_query("SHOW TABLES FROM " . DB_NAME, $dbhandle) or die(mysql_error());
and
$result_fld = mysql_query("SHOW FIELDS FROM $table", $dbhandle) or die(mysql_error());