在将数据传递到SQL数据库以获取JSON输出时,不接收PHP文件的文本输出

时间:2014-07-14 09:19:06

标签: php mysql

已经设置了一个PHP文件,以便联系SQL数据库,并使用我附加到URL末尾的相应ArticleID检索文章。

例如,www.example.com/retrieveArticle.php?ArticleID = 1

目前,网页没有显示任何文本输出,而我希望找到一行输出为JSON格式。

请注意,我的表名为Customer。

代码

<?php
if(issert($_REQUEST["ArticleID"]))
    {
    $con = mysql_connect("localhost","createyo_james","password");
    if(!$con)
    {
    die("Could not connect: " .mysql_error());
    }
    mysql_select_db("createyo_TestDatabase", $con);

    $articleID = $_REQUEST["ArticleID"];

    $result = mysql_query("SELECT * FROM Customer WHERE ID = "$articleID" ") or die("Errant query:");

    while($row = mysql_fetch_assoc($result))
    {
    $output[]=$row;
    }

    print(json_encode($output));

    mysql_close($con);

    }

    else
    {
    $output = "not found";
    print(json_encode($output));
    }

?>

修改

由于许多解决方案都没有奏效,可能是由于数据库信息不正确。我知道连接细节是正确的,因为我有类似的PHP文件连接到同一个DB。但是,在获取ID等时,我可能对表结构有误......

Database

代码尝试2

<?php
if(isset($_GET["ArticleID"]))
{
$con = mysql_connect("localhost","createyo_james","password");
if(!$con)
{
die("Could not connect: " .mysql_error());
}
mysql_select_db("createyo_TestDatabase", $con);
$articleID = mysql_real_escape_string($_GET["ArticleID"]);

$result = mysql_query("SELECT * FROM Customer WHERE ID = "$ArticleID"") or die("Errant query:");

while($row = mysql_fetch_assoc($result)) { array_push($output,$row); } echo json_endode($output);die;

mysql_close($con);

}

else
{
$output = "not found";
echo json_encode($output);
}

?>

3 个答案:

答案 0 :(得分:1)

第一行有拼写错误:

if(issert($_REQUEST["ArticleID"]))

应该是:

if(isset($_REQUEST["ArticleID"]))

刚注意到别的东西。您的查询:

$result = mysql_query("SELECT * FROM Customer WHERE ID = "$articleID" ") or die("Errant query:");

$ articleID周围不应包含双引号:

$result = mysql_query("SELECT * FROM Customer WHERE ID = '$articleID' ") or die("Errant query:");

答案 1 :(得分:0)

已编辑的代码

<?php

if (isset($_GET["id"])) {
    $con = mysql_connect("localhost", "USER_NAME", "PASSWORD");
    if (!$con) {
        die("Could not connect: " . mysql_error());
    }
    if (!mysql_select_db("DB_NAME", $con)) {
        die("Database error " . mysql_error());
    }
    $id = $_GET["id"];
    $query="SELECT * FROM table WHERE ID =".$id;
    $result = mysql_query($query) or die("Errant query:");
    $output = Array();
    while ($row = mysql_fetch_assoc($result)) {
        $output[] = $row;
    }
    echo json_encode($output);
    mysql_close($con);
    die;
} else {
    $output = "not found";
    echo (json_encode($output));
    die;
}
?>

答案 2 :(得分:0)

<?php
if(isset($_GET["ArticleID"]))
    {
    $con = mysql_connect("localhost","createyo_james","password");
    if(!$con)
    {
    die("Could not connect: " .mysql_error());
    }
    mysql_select_db("createyo_TestDatabase", $con);

    $articleID = mysql_real_escape_string($_GET["ArticleID"]);

    $result = mysql_query("SELECT * FROM Customer WHERE ID = '$articleID' ") or die("Errant query:");

    while($row = mysql_fetch_assoc($result))
    {
$id = $row['id'];
    $output[]= array('id' => "$id");
    }

    echo json_encode($output);

    mysql_close($con);

    }

    else
    {
    $output = "not found";
    echo json_encode($output);
    }

?>