MYSQL_STMT :: bind_result()

时间:2014-05-28 18:15:58

标签: php mysql subquery

我有以下mysql查询,它在我的php脚本中执行:

$qr = "Select p.productID,p.productDesc,p.productQty,";
$qr .= "p.productPr,p.type,p.gender,p.date From";
$qr .= " products AS p";
$qr .= " INNER JOIN(Select c.productID, GROUP_CONCAT(";
$qr .= "DISTINCT c.availCol) AS color_list FROM";
$qr .= " availColors AS c GROUP BY c.productID) AS colors";
$qr .= " ON p.productID = colors.productID":
$qr .= " INNER JOIN(SELECT s.productID, GROUP_CONCAT";
$qr .= "(s.availSizes) AS size_list FROM availSizes AS s";
$qr .= " GROUP BY s.productID) AS sizes ON p.productID";
$qr .= " = sizes.productID";
$qr .= " INNER JOIN(SELECT avp.productID, avp.productImg";
$qr .= " FROM availImg AS avp ORDER BY avp.productID";
$qr .= " LIMIT 3) AS images ON images.productID = ";
$qr .= " p.productID";
$qr .= " WHERE p.productID = ?";
$qr .= " GROUP BY p.productID";

$stm = $mysqli->prepare($qr);
$stm->bind_param('s',$id);
$stm->execute();
$stm->store_result();

If($stm->num_rows == 1){
$stm->bind_result($pid,$desc,$qty,$pr,$type,$gender,
$date,$colID,$color,$sizeID,$sizes,$imgID,$imgUrl);

$stm->fetch();
......
}

当我执行脚本时,我收到一条错误消息:     Mysqli_stmt :: bind_result():绑定变量的数量不对     匹配准备好的语句中的字段数...

我理解这个错误意味着什么,但是我已经重新检查并重新计算了bind_result()中绑定变量的数量与SELECTed列的数量,我只是无法弄清楚是什么问题。我不知道PHP如何评估从mysql子查询返回的值。有人可以提供帮助吗?我整天都在这!谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

$qr = "
Select
    p.productID,
    p.productDesc,
    p.productQty,
    p.productPr,
    p.type,
    p.gender,
    p.date
From
    products AS p
    INNER JOIN(
        Select 
            c.productID, 
            GROUP_CONCAT(DISTINCT c.availCol) AS color_list 
        FROM
            availColors AS c GROUP BY c.productID) AS colors
            ON
                p.productID = colors.productID
        INNER JOIN(
            SELECT
                s.productID,
                GROUP_CONCAT(s.availSizes) AS size_list
            FROM
                availSizes AS s
            GROUP BY
                s.productID
        ) AS sizes
    ON
        p.productID = sizes.productID
        INNER JOIN(
            SELECT
                avp.productID,
                avp.productImg
            FROM
                availImg AS avp
            ORDER BY
                avp.productID
            LIMIT
        3) AS images
    ON
        images.productID =  p.productID
WHERE
    p.productID = ?
GROUP BY
    p.productID";

尝试格式化您的查询,就像这样多行字符串在PHP中完全没问题,并且几乎不可能逐行理解它。如果我说得对,你至少缺少一个右括号,那么第一个内连接开括号就不会关闭。

答案 1 :(得分:0)

您只是加入其他表/列而不选择列。尝试将列添加到查询的开头(FROM之前)

$qr = "SELECT p.productID,p.productDesc,p.productQty,";
$qr .= "p.productPr,p.type,p.gender,p.date,";

// colors columns
$qr .= "colors.productID, colors.color_list,";
// sizes columns
$qr .= "sizes.productID, sizes.size_list,";
// images columns
$qr .= "images.productID, images.productImg ";

$qr .= "From products AS p";
$qr .= " INNER JOIN(Select c.productID, GROUP_CONCAT(";
$qr .= "DISTINCT c.availCol) AS color_list FROM";
$qr .= " availColors AS c GROUP BY c.productID) AS colors";
$qr .= " ON p.productID = colors.productID":
$qr .= " INNER JOIN(SELECT s.productID, GROUP_CONCAT";
$qr .= "(s.availSizes) AS size_list FROM availSizes AS s";
$qr .= " GROUP BY s.productID) AS sizes ON p.productID";
$qr .= " = sizes.productID";
$qr .= " INNER JOIN(SELECT avp.productID, avp.productImg";
$qr .= " FROM availImg AS avp ORDER BY avp.productID";
$qr .= " LIMIT 3) AS images ON images.productID = ";
$qr .= " p.productID";
$qr .= " WHERE p.productID = ?";
$qr .= " GROUP BY p.productID";

答案 2 :(得分:0)

由于MYSQL Workbench,我终于明白了!我在mysql workbench上运行查询并计算返回的列数,并在bind_result()上为每个列声明变量。