简体   繁体   中英

Ajax XML Error When Using PHP PDO

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>';
?>

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>';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM