I have a php script which…
- Deletes a record from a MySql table (based on the id) via PDO
- Get’s an updated list of all the records in that table, then…
- Spits out the list as XML (via DomDocument)
The Problem is that when I run the script I see the following error message when I check the XHR tab for XML:
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:
^
When looking at the Response section of the XHR tab in Firebug, I noticed that the xml starts below the top of the page. On the third row to be exact.
So… I made the call directly from a browser including the id of a record to be deleted
( i.e. http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40 )
Wherein I then got (pretty much the same error (this time in the browser):
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:
^
Below is the code I’m using: (note that if I simply take out the call to the db, it works. So I am assuming that something I am doing is creating the extra spaces, but dang if I can find it).
<?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();
?>
I don’t get. By the way, did I mention that I’m a greenhorn?
🙂
thanks
sleeper
I found it. It WAS a space. Brought on by seperate tags for FirePHP. Removed them and whalla!