0

我是 php 的超级新手,所以我需要一些帮助。我玩得很开心……我知道这很简单!

我在这里有一个 php 文件:http: //theclackamasprint.net/json.php

它将信息从 mysql 提取到 json。

我想要一个解析 URL 以通过它们的 catid 分隔文章(未显示)

所以我希望能够输入 http://theclackamasprint.net/json.php?catid=84 http://theclackamasprint.net/json.php?catid=87

等等......只有那些猫秀

这是我的代码:

$host = "******";
$db = "******";
$user = "******";
$pass = "******";

$connection = mysql_connect($host, $user, $pass);

//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");  
}
else
{
    //Attempt to select the database
    $dbconnect = mysql_select_db($db, $connection);

    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $query = "SELECT title FROM tcp_content WHERE catid=84 ORDER BY publish_up DESC LIMIT 20";
        $resultset = mysql_query($query, $connection);

        $records = array();

        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records[] = $r;        
        }

        //Output the data as JSON
        echo json_encode($records);
    }


}
4

1 回答 1

-1

最短的解决方案涉及更改catid=84catid=" . $_GET['catid'] . ",从而将 catid 参数插入到 MySQL 查询中。

结果线看起来像

$query = "SELECT title FROM tcp_content WHERE catid=" . $_GET['catid'] . " ORDER BY publish_up DESC LIMIT 20";

然而,这很容易受到 SQL 注入的影响,所以如果这是为了任何严肃的工作,请务必切换到mysqli_query()PDO::query()

于 2013-11-08T05:25:03.803 回答