집 >데이터 베이스 >MySQL 튜토리얼 >PHP를 사용하여 MySQL 데이터베이스에서 XML 출력을 생성하는 방법은 무엇입니까?
PHP를 사용하여 MySQL 데이터베이스에서 XML 출력 생성
MySQL 데이터베이스에서 XML 데이터를 추출해야 합니까? 특정 열을 검색하려면 PHP를 사용하여 다음 단계를 따르세요.
1단계: 데이터베이스에 연결
mysql_connect('server', 'user', 'pass'); mysql_select_db('database');
2단계: 데이터베이스 쿼리
$sql = "SELECT udid, country FROM table ORDER BY udid"; $res = mysql_query($sql);
3단계: XML 작성기 생성
$xml = new XMLWriter(); $xml->openURI("php://output"); $xml->startDocument(); $xml->setIndent(true);
4단계: XML 구조 시작
$xml->startElement('countries');
5단계: 결과 반복
while ($row = mysql_fetch_assoc($res)) { $xml->startElement("country"); $xml->writeAttribute('udid', $row['udid']); $xml->writeRaw($row['country']); $xml->endElement(); }
6단계: XML 구조 종료
$xml->endElement();
7단계: 출력 헤더 설정 및 Flush XML
header('Content-type: text/xml'); $xml->flush();
출력 예:
<countries> <country udid="1">Country 1</country> <country udid="2">Country 2</country> ... <country udid="n">Country n</country> </countries>
이 코드는 MySQL 테이블의 지정된 열을 포함하는 XML 출력을 생성합니다. 필요에 따라 SQL 쿼리를 사용자 정의하여 다양한 열을 검색할 수 있습니다.
위 내용은 PHP를 사용하여 MySQL 데이터베이스에서 XML 출력을 생성하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!