使用PHP PDO时出现Ajax XML错误

时间:2014-01-16 14:27:17

标签: php ajax pdo

我最近将用于访问数据库的代码切换为PHP PDO对象。 我有一切工作接受我的ajax页面。至于我已经能够告诉所有的查询和数据被正确拉出,但是当我尝试使用PDO时,我得到以下错误,这是在使用mysql_connect对象之前。 我确实发现如果我将这些行注释掉它会运行但是它无法运行查询,这会导致更多错误。

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

错误讯息:

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课程查询功能

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

1 个答案:

答案 0 :(得分:0)

最后通过移动include for bio.php来确定它的工作原因不确定为什么现在可以使用而且之前不会。

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