Note that there are some explanatory texts on larger screens.

plurals
  1. PODomDocument XML NOT starting at 1st Line. Causing XML Malformed Error
    text
    copied!<p>I have a php script which...</p> <ol> <li>Deletes a record from a MySql table (based on the id) via PDO</li> <li>Get's an updated list of all the records in that table, then...</li> <li>Spits out the list as XML (via DomDocument)</li> </ol> <p><strong>The Problem</strong> is that when I run the script I see the following <strong>error message</strong> when I check the XHR tab for XML: </p> <blockquote> <p><strong>XML Parsing Error:</strong> XML or text declaration not at start of entity Location: moz-nullprincipal:{45b1a3cb-ef07-42c9-b4bd-b15ba996fb4c} Line Number 3, Column 1:</p> <p> ^</p> </blockquote> <p>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.</p> <p>So... I made the call directly from a browser including the id of a record to be deleted ( i.e. <a href="http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40" rel="nofollow">http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40</a> )</p> <p>Wherein I then got (pretty much the same error (this time in the browser):</p> <blockquote> <p>XML Parsing Error: XML or text declaration not at start of entity Location: <a href="http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40" rel="nofollow">http://mylocalsite.dev/ajax-delete-post-v02.php?dlt=40</a> Line Number 3, Column 1: ^</p> </blockquote> <p>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).</p> <pre><code>&lt;?php $doc = new DOMDocument('1.0','utf-8'); $posts = $doc-&gt;createElement('posts'); $doc-&gt;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-&gt;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-&gt;query($sql_select); while($row = $stmt-&gt;fetch()) { $post = $doc-&gt;createElement('post'); $post-&gt;setAttribute('id',$row['id']); $p_text = $doc-&gt;createElement('text',htmlentities($row['text'])); $p_date_start = $doc-&gt;createElement('date_start',$row['date_start']); $p_date_end = $doc-&gt;createElement('date_end',$row['date_end']); $p_details_link = $doc-&gt;createElement('details_link',htmlentities($row['details_link'])); $posts-&gt;appendChild($post); $post-&gt;appendChild($p_text); $post-&gt;appendChild($p_date_start); $post-&gt;appendChild($p_date_end); $post-&gt;appendChild($p_details_link); } // Close the db connection $dbh = null; } catch(PDOException $e) { echo $e-&gt;getMessage(); } // for some reason this is writing it out starting on the third line??? echo $doc-&gt;saveXML(); ?&gt; </code></pre> <p>I don't get. By the way, did I mention that I'm a greenhorn? </p> <p>:)</p> <p>thanks sleeper</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload