donglian1523 2014-07-14 09:19
浏览 61
已采纳

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

A PHP file has been set up in order to contact a SQL database and retrieve the article with the corresponding ArticleID which I append to the end of the URL.

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

Currently, the webpage does not show any text output whereas I expected to find one row output as a JSON format.

Note that my table is called Customer.

Code

<?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));
    }

?>

Edit:

Since many of the solutions have not worked it may be due to incorrect database information. I know the connection details are correct as I have similar PHP files connected to the same DB. However, I might be wrong about the table structure when getting ID etc...

Database

Code Attempt 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条回答 默认 最新

  • douwei8096 2014-07-14 09:25
    关注

    Edited Code

    <?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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题