0

我有这个代码:

function getPricing() {
$returnarr = array();
$dcid = (isset($_REQUEST['dcid'])) ? "WHERE datacentre_id='" . mysql_real_escape_string($_REQUEST['dcid']) . "'" : '';

$addons = $_REQUEST['addon'];

if ($addons == "true")
    $sql = "SELECT * FROM addonprices $dcid ORDER BY addon_desc ASC";
else
    $sql = "SELECT l.l_name, p.* FROM pricing p LEFT JOIN locations l ON l.location_id=p.datacentre_id $dcid ORDER BY CASE splitsize WHEN 'f' THEN 0 WHEN 'w' THEN 1 WHEN 'h' THEN 2 WHEN 'q' THEN 3 WHEN '2U' THEN 4 WHEN '1U' THEN 5 ELSE 6 END DESC, amps ASC";

$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    $returnarr[] = $row;
}
header('Content-type: application/json');
echo json_encode($returnarr);
}

我需要在两个 select 语句中添加另一个表,加入$dcid,表被调用slas,列是currency我尝试了一些事情但没有运气。

我正在尝试将货币拉入 javascript 文件以使用此 switch 语句获取价格:

var currency = "";

            switch (row.currency) {
                case '1' :
                    currency = "£";
                    break;
                case '2' :
                    currency = " $";
                    break;
                case '3' :
                    currency = "€";
                    break;
                default:
                    currency = "£";
                    break;


            }

即使货币应该等于 2 或 3,它仍然显示为默认值......我不确定是 switch 语句错误还是 sql 查询。

更新

只是对这一行有问题:

$sql = "SELECT l.l_name, p.*, slas.currency FROM pricing p LEFT JOIN locations l ON l.location_id=p.datacentre_id LEFT JOIN slas ON slas.datacentre_id = p.datacentre_id $dcid ORDER BY CASE splitsize WHEN 'f' THEN 0 WHEN 'w' THEN 1 WHEN 'h' THEN 2 WHEN 'q' THEN 3 WHEN '2U' THEN 4 WHEN '1U' THEN 5 ELSE 6 END DESC, amps ASC";

摘自下面的答案。

4

1 回答 1

0

我认为这里的问题是datacentre_id您的不合格WHERE将不允许您加入另一个表,其中的列名为datacentre_id

我建议在哪里

$dcid = (isset($_REQUEST['dcid'])) ? "WHERE p.datacentre_id='" . mysql_real_escape_string($_REQUEST['dcid']) . "'" : '';

并且查询是

if ($addons == "true")
    $sql = "SELECT p.*, slas.currency FROM addonprices p LEFT JOIN slas ON slas.datacentre_id = p.datacentre_id $dcid ORDER BY addon_desc ASC";
else
    $sql = "SELECT l.l_name, p.*, slas.currency FROM pricing p LEFT JOIN locations l ON l.location_id=p.datacentre_id LEFT JOIN slas ON slas.datacentre_id = p.datacentre_id $dcid ORDER BY CASE splitsize WHEN 'f' THEN 0 WHEN 'w' THEN 1 WHEN 'h' THEN 2 WHEN 'q' THEN 3 WHEN '2U' THEN 4 WHEN '1U' THEN 5 ELSE 6 END DESC, amps ASC";

请注意,在这两种情况下,其中的表datacentre_id都是别名 p

于 2013-07-29T10:22:05.970 回答