2011-08-22 5 views
2

PHP로 SQL 데이터베이스를 구문 분석하여 XML을 출력 할 수 있기를 원하지만 테이블 이름과 모든 내용을 표시 할 수 없습니다. 나는 약간의 도움을 사용할 수있다. 여기에 로그인 정보가없는 코드가있다. 나는 db 서버, mdb 사용자와 패스 그리고 db 이름을 정의했다; 다른 정의해야합니까?PHP XML 구문 분석 문제

<?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); 
?> 
+0

작동하지 않는 기능은 무엇입니까? – hakre

+0

테이블 이름은 표시되지만 내용은 표시되지 않습니다. – JAckie

답변

2

표준 PHP 클래스 인 XmlWriter를 사용하는 것이 좋습니다. http://www.php.net/manual/en/book.xmlwriter.php

+0

각 열을 선언하지 않고 테이블의 모든 데이터를 출력하는 방법을 알고 있습니까? 20 이상입니다. – JAckie

+0

코드에서와 같은 루프를 사용할 수 있지만 대신 $ output. = "

1
$result_tbl = mysql_query("SHOW TABLES FROM "DB_NAME, $dbhandle); 
              ^^^---typo 

$result_fld = mysql_query("SHOW FIELDS FROM "$table, $dbhandle); 
              ^^^---typo 

에서 봐 당신은 쿼리에 두 개 이상의 오타가 있어요. 대부분의 경우 당신이 원하는 :

$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()); 

주 첫 번째에 연결 연산자 (.), 및 or die(...)의 추가. 쿼리가 성공했다고 가정하지 마십시오. 질의 문자열 자체가 구문 적으로 정확하더라도, 너무 많은 다른 이유가 오류 조건을 확인하지 못할 수 있습니다.

+0

출력으로이 오류가 표시됩니다. SQL 구문에 오류가 있습니다. 귀하의 MySQL 서버 버전에 해당하는 설명서를 확인하여 올바른 구문이 1 행에서 'table name here'근처에서 사용되도록하십시오. – JAckie

+0

생성 된 쿼리 문은 어떻게 생겼습니까? '$ sql = "..."; mysql_query ($ sql) 또는 die ("Error". mysql_error(). $ sql);' –