1

我的问题是,当我直接在 MySQL 中运行查询时,它可以工作:

SELECT * FROM SubCategoryLookup  WHERE LinkedCategoryID = '4' AND OrganizationCode = 'ibm'

在使用 PHP 时,如果我给出任何变量,那么它不会得到记录:

$OrganizationCode=$_REQUEST['OrganizationCode'];
echo($OrganizationCode);

$CategoryId=$_REQUEST['CategoryId'];
echo($CategoryId);

$query = mysql_query("SELECT *
                        FROM SubCategoryLookup
                       WHERE LinkedCategoryID = '$CategoryId'
                             AND OrganizationCode = '$OrganizationCode'");
echo($query);

$rows = array();
while($row = mysql_fetch_assoc($query))
{
    $rows[] = $row;
}
echo json_encode($rows);

当我使用 URL 访问它时:

http://www.celeritas-solutions.com/pah_brd_v1/productivo/getSubCategory.php?OrganizationCode=celeritasCategoryId=3

下面是它不显示记录的结果

celeritasCategoryId=3Resource id #3[]
4

2 回答 2

1

您的网址上缺少一个&,应该是:

http://www.celeritas-solutions.com/pah_brd_v1/productivo/getSubCategory.php?OrganizationCode=celeritas&CategoryId=3

仔细观察,你有:

OrganizationCode=celeritasCategoryId=3

它应该是:

OrganizationCode=celeritas&CategoryId=3

这会导致$CategoryId未设置(或换句话说为空),这会使您的查询失败并且没有结果。

您应该使用isset确保您的变量已设置:

$OrganizationCode = $_GET['OrganizationCode'];
if (!isset($OrganizationCode))
    die("There is no organization code...");

$CategoryId = $_GET['CategoryId'];
if (!isset($CategoryId))
    die("There is no category id...");

以及使用mysql-real-escape-string和正确使用错误处理程序对其进行清理以防止注入,以了解查询是否失败mysql_error

$sql = sprintf("SELECT * 
                  FROM SubCategoryLookup 
                 WHERE LinkedCategoryID='%s' AND 
                       OrganizationCode='%s'",
               mysql_real_escape_string($CategoryId),
               mysql_real_escape_string($OrganizationCode));

$query = mysql_query($sql);

// change $link to the name of your database connection variable
if (!$query)
    die('Query failed (' . mysql_errno($link) . '): ' . mysql_error($link));

不再支持功能mysql_*,它们已正式弃用不再维护,将来将被删除您应该使用PDOMySQLi更新您的代码,以确保您的项目在未来的功能。


这就是您将得到的:

 [{
     "SubCategoryID": "8",
     "LinkedCategoryID": "3",
     "SubCategoryTitle": "Projects: Client A",
     "SubCategoryAddedDateTime": "2013-07-12 23:16:25",
     "SubCategoryAddedByUserID": "1",
     "OrganizationCode": "celeritas"
 }, {
     "SubCategoryID": "9",
     "LinkedCategoryID": "3",
     "SubCategoryTitle": "Projects: Client B",
     "SubCategoryAddedDateTime": "2013-07-12 23:16:25",
     "SubCategoryAddedByUserID": "1",
     "OrganizationCode": "celeritas"
 }, {
     "SubCategoryID": "10",
     "LinkedCategoryID": "3",
     "SubCategoryTitle": "Projects: Client C",
     "SubCategoryAddedDateTime": "2013-07-12 23:16:25",
     "SubCategoryAddedByUserID": "1",
     "OrganizationCode": "celeritas"
 }]
于 2013-07-13T08:09:23.053 回答
0

确保您的 $rows 数组是正确的。在执行 json_encode 之前打印它。

删除您的数组分配代码并添加此

array_push($rows,$row)

然后打印 $rows

于 2013-07-13T07:48:30.823 回答