0

I recently switched my code for accessing my database to a PHP PDO Object. I have everything working accept for my ajax page. As far as I have been able to tell all the queries and data are being pulled out properly, however I get the following error when I try using PDO this was working before with a mysql_connect object. I did find that if I comment out these lines it will run but then it is unable to run the query which causes more errors obviously.

//ini_set('include_path', 'C:\www\capc\libraries');
//include '/php/capc.php';
//include '/php/bio.php'; 

Error Message:

This page contains the following errors:
error on line 4 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

CAPC Class query function

public function query($sql) {
    try {
        $handler = new PDO('mysql:host=' . $this->dbhost . ';dbname=capc', $this->dbuser, $this->dbpass);
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    $query = $handler->prepare($sql);
    $query->execute();
    return $query;
}

Ajax.php

<?php
ini_set('include_path', 'C:\www\capc\libraries');
include '/php/capc.php';
include '/php/bio.php';
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
$bio_first = $_GET['First_Name'];
$bio_last = $_GET['Last_Name'];
if (!empty($bio_first) && !empty($bio_last)) {
$capc = new CAPC;
$sql = 'SELECT Bio_ID FROM `bio_users` WHERE Bio_First = "' . $bio_first . '" AND Bio_Last = "' . $bio_last . '";';
$query = $capc->query($sql);
$num_results = $query->rowCount();
}

echo '<bio>';
if ($num_results > 0) {
$bio_first = $capc->sanitize($bio_first, "string");
$bio_last = $capc->sanitize($bio_last, "string");
$bio = new Bio($bio_first, $bio_last);
echo '<bio_exists>';
echo 'True';
echo '</bio_exists>';
echo '<bio_fname>';
echo $bio->Bio_First;
echo '</bio_fname>';
echo '<bio_lname>';
echo $bio->Bio_Last;
echo '</bio_lname>';
echo '<bio_img>';
echo $bio->Bio_Img;
echo '</bio_img>';
} else {
echo '<bio_exists>';
echo 'False';
echo '</bio_exists>';
}
echo '</bio>';
?>
Talon06
  • 1,756
  • 3
  • 27
  • 51

1 Answers1

0

Finally got it working by moving the include for bio.php not sure why this works now and wouldn't before.

Working Ajax.php

<?php
ini_set('include_path', 'C:\www\capc\libraries');
include '/php/capc.php';

$bio_first = $_GET['First_Name'];
$bio_last = $_GET['Last_Name'];
if (!empty($bio_first) && !empty($bio_last)) {
$capc = new CAPC;
$sql = 'SELECT Bio_ID FROM `bio_users` WHERE Bio_First = "' . $bio_first . '" AND Bio_Last = "' . $bio_last . '";';
$query = $capc->query($sql);
$num_results = $capc->query($sql)->rowCount();
}
if ($num_results > 0) {
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<bio>';
$bio_first = $capc->sanitize($bio_first, "string");
$bio_last = $capc->sanitize($bio_last, "string");
include '/php/bio.php';
$bio = new Bio($bio_first, $bio_last);
echo '<bio_exists>';
echo 'True';
echo '</bio_exists>';
echo '<bio_fname>';
echo $bio->Bio_First;
echo '</bio_fname>';
echo '<bio_lname>';
echo $bio->Bio_Last;
echo '</bio_lname>';
echo '<bio_img>';
echo $bio->Bio_Img;
echo '</bio_img>';
} else {
echo '<bio_exists>';
echo 'False';
echo '</bio_exists>';
}
echo '</bio>';
?>
Talon06
  • 1,756
  • 3
  • 27
  • 51
  • You got a comment with the explanation at the very moment you posted the question. – Álvaro González Jan 16 '14 at 15:43
  • Still doesn't make sense why moving one include should change change anything? – Talon06 Jan 16 '14 at 15:46
  • Including a file is exactly the same as pasting its contents at that precise point. If the file generates output (such as error messages or blank lines) the output will be there. Remember that PHP can generate output without `echo` statements, e.g.: `Output` – Álvaro González Jan 16 '14 at 15:53
  • K thanks for the info, didn't register in my head to check the other includes inside bio.php – Talon06 Jan 16 '14 at 16:11