以下是我得到的错误,此页面包含以下错误:
第 1 列第 1 行错误:文档末尾有额外内容 第 1 列第 1 行错误:编码错误
下面是第一个错误之前的页面渲染。
以下是代码,
<?php
if (isset($_GET['itemno'])) {
$number_of_posts = isset($_GET['number']) ? intval($_GET['number']) : 20;
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml';
$user_id = intval($_GET['itemno']);
/* connect to mysql database */
$link = mysql_connect('localhost', 'root', '') or die('Can not connect to the Database');
mysql_select_db('test', $link) or die('Can not select the Database');
/* select records from the database */
$query = "SELECT qty, price FROM postatusinfo WHERE itemno = $user_id";
$result = mysql_query($query, $link) or die('Errant query: ' . $query);
/* create array of the records */
$posts = array();
if (mysql_num_rows($result)) {
while ($post = mysql_fetch_assoc($result)) {
$posts[] = array('post' => $post);
}
}
/* output in required format */
if ($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts' => $posts));
} else {
header('Content-type: text/xml');
echo '<posts>';
foreach ($posts as $index => $post) {
if (is_array($post)) {
foreach ($post as $key => $value) {
echo '<', $key, '>';
if (is_array($value)) {
foreach ($value as $tag => $val) {
echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
}
}
echo '</', $key, '>';
}
}
}
echo '</posts>';
}
/* close database connection */
@mysql_close($link);
}