SQL Script在phpAdmin中工作,但在网站echo上不起作用

时间:2017-07-05 20:37:40

标签: php mysql

以下SQL脚本在phpMyAdmin的SQL编辑器中工作。但是,在PHP中我无法使用此脚本在线显示真实内容。这一切都是空白的。问题出在我的回声中。我尝试使用$row['a.qty'],但这不起作用。

$sql = "SELECT  a.qty + b.qty - c.qty as 'QTY', a.part_num as 'Part Num', a.part_desc as 'Description'
        FROM    Inv_Physical_Count a,
                Inv_Restock b,
                Inv_Orders c
        WHERE   a.part_id = b.part_id
        AND     a.part_id = c.part_id
        ORDER BY a.order_form_seq";

        $q = $pdo->prepare($sql);
        $q->execute(array());

        while ($row = $q->fetch(PDO::FETCH_ASSOC)) 
        {
            echo    '<tr>';
            echo    '<td>' . $row['qty'] . '</td>';
            echo    '<td>' . $row['part_num'] . '</td>';
            echo    '<td>' . $row['part_desc'] . '</td>';               
        }

1 个答案:

答案 0 :(得分:4)

首先,返回勾选(不引用)您的AS值,特别是如果它们包含空格:

$sql = "SELECT  a.qty + b.qty - c.qty as `QTY`, a.part_num as `Part Num`, a.part_desc as `Description`

您告诉查询这些值应该是列名。因此,使用AS值作为数组标识符:

while ($row = $q->fetch(PDO::FETCH_ASSOC)) 
{
    echo    '<tr>';
    echo    '<td>' . $row['QTY'] . '</td>';
    echo    '<td>' . $row['Part Num'] . '</td>';
    echo    '<td>' . $row['Description'] . '</td>';               
}