MySQL-PHP-XML输出

时间:2016-02-08 15:11:46

标签: php mysql xml

我是一个相对缺乏经验的程序员,试图通过当前的项目。基本上,我正在为数据库设计一个功能;使用搜索查询,某人将能够将该信息插入到套用信函中。此时,我正在尝试使用XMLWriter将搜索结果转换为xml格式,遗憾的是搜索页面甚至不再生成。我正在使用基于提供的解决方案的代码我已经包含下面的PHP代码,欢迎任何和所有反馈

    <?php 

    session_start(); 
    include 'conn.php'; 

    if(empty($_SESSION['username']) || empty($_SESSION['password'])) 
        print("Access to database denied"); 
    else { 
        $username = $_SESSION['username']; 
        $password = $_SESSION['password']; 
        $type = $_SESSION['type']; 

        if($type == "admin") { 
            include '../includes/aheader.html'; 
        } 
        if($type == "user") { 
            include '../includes/uheader.html'; 
        } 
        if(isset($_POST["searchButton"])) { 
            $keyword = $_POST['keyword']; 
            $choice = $_POST['choice'];

        if($choice == "company_name") 
            $sql = $mysqli -> prepare("SELECT * FROM clients WHERE company_name LIKE ?"); 

        if($choice == "project_code") 
            $sql = $mysqli -> prepare("SELECT * FROM clients WHERE project_code LIKE ?"); 

        $keyword = '%'.$keyword.'%'; 
        $sql -> bind_param('s', $keyword); 
        $sql -> execute(); 
        $result = $sql -> get_result(); 

        if(!$result) 
                print("<p>Select query failed</p>"); 
            else { 
        if($result -> num_rows == 0) 
                print("<p>No match found</p>");
            else {
                header("Content-Type: text/html/force-download");
                header("Content-Disposition: attachment; filename='file_name.xml'");

                $xml = new XMLWriter();

                $xml -> openURI("test.xml");
                $xml -> startDocument("1.0", "UTF-8");
                $xml -> setIndent(true);

                while($row = mysql_fetch_assoc($results)) {
                    $xml -> startElement("clients");

                    $xml -> writeAttribute('company_name', $row['company_name']);
                    $xml -> writeAttribute('phone', $row['phone']);
                    $xml -> writeAttribute('address', $row['address']);
                    $xml -> writeAttribute('approximate_employees', $row['approximate_employees']);
                    $xml -> writeAttribute('project_code', $row['project_code']);
                    $xml -> writeRaw($row['clients']);

                    $xml -> endElement();
               }

               $xml -> endElement();

               header('Content-type: text/xml');
               $xml -> flush();
            }
        }
    }
}
?>

0 个答案:

没有答案