我通過PDO有一個PHP腳本...DomDocument XML不從第1行開始。造成畸形XML錯誤
- 從MySQL表(基於ID)刪除記錄
- 獲取是在該表中所有記錄的更新列表,然後...
- 吐出列表作爲XML(通過的DomDocument)
的問題是,當我運行該腳本我看到下面的錯誤信息當我檢查XML的XHR標籤:
XML Parsing Error: XML or text declaration not at start of entity Location: moz-nullprincipal:{45b1a3cb-ef07-42c9-b4bd-b15ba996fb4c} Line Number 3, Column 1:
^
當看着螢火蟲的XHR選項卡的響應部分,我注意到,XML開始後面的頁面的頂部。準確地說第三行。
所以......我直接從瀏覽器發出的呼籲,包括要在瀏覽器 (即http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40)
其中,然後我得到了(幾乎是同樣的錯誤(這一次刪除的記錄的ID ):
XML Parsing Error: XML or text declaration not at start of entity Location: http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40 Line Number 3, Column 1: ^
下面是我使用的代碼。(注意,如果我只是拿出調用數據庫,它的工作原理所以我假設的東西我做的是創造了多餘的空格,但是當我能找到它時)。
<?php
$doc = new DOMDocument('1.0','utf-8');
$posts = $doc->createElement('posts');
$doc->appendChild($posts);
// dynamically generate posts
// set DB connection vars
$hostname = "localhost";
$database = "somedb";
$username = "someuser";
$password = "somepassword";
$hostinfo = "mysql:host=$hostname;dbname=$database";
// connect to db
try
{
// Create the database handler
$dbh = new PDO($hostinfo,$username, $password);
// Set the error reporting attributes
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create the sql string
$sql_select = 'SELECT * FROM posts ORDER BY date_start DESC';
// prepare the SQL statement
$stmt = $dbh->query($sql_select);
while($row = $stmt->fetch())
{
$post = $doc->createElement('post');
$post->setAttribute('id',$row['id']);
$p_text = $doc->createElement('text',htmlentities($row['text']));
$p_date_start = $doc->createElement('date_start',$row['date_start']);
$p_date_end = $doc->createElement('date_end',$row['date_end']);
$p_details_link = $doc->createElement('details_link',htmlentities($row['details_link']));
$posts->appendChild($post);
$post->appendChild($p_text);
$post->appendChild($p_date_start);
$post->appendChild($p_date_end);
$post->appendChild($p_details_link);
}
// Close the db connection
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
// for some reason this is writing it out starting on the third line???
echo $doc->saveXML();
?>
我不明白。順便說一下,我提到我是新手?
:)
感謝 臥鋪
在打開PHP標籤之前是否有換行符?因爲不在之間的所有內容都被視爲普通文本。 –
@ Jan-Henk Nope。衝上頂部。沒有新的線路。 – sleeper
我是否將此信息發佈到正確的組?沒意見? – sleeper