while($ row = mysql_fetch_array($ result))不起作用

时间:2012-10-19 04:26:46

标签: php mysql while-loop

我使用以下代码获取一些数据。

    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)) {
        $data[] = array(
            $row['Qty'], 
            $row['Description'] 
        );      
    }

这是一个空数组。

当我手动尝试$sql时,它会获得正确的结果。

上面的代码有什么问题吗??

SELECT CO.order_id AS TaxNo, 
DATE_FORMAT(CO.created,'%d-%m-%Y') AS InvoiceDate, 
C.company_name AS Consumer, CONCAT(C.delivery_address1, ', ', 
C.delivery_address2) AS DelAdd, 
COD.qty AS Qty, 
P.product_name AS Description, 
PP.amount AS Price, 
(COD.qty * PP.amount) AS Total, 
SUM(COD.qty * PP.amount) AS InvoiceTotal, 
CPH.discount_on_payment AS Discount, 
CPH.amount_paid AS AmountPaid, 
(SUM(COD.qty * PP.amount) - CPH.discount_on_payment - CPH.amount_paid) AS Balance,  
COUNT(COD.qty) AS TotLine 
FROM consumer_orders AS CO 
INNER JOIN consumer AS C ON CO.consumer_id = C.consumer_id 
INNER JOIN consumer_order_details AS COD ON CO.order_id = COD.order_id 
INNER JOIN product AS P ON COD.Product_id = P.product_id 
LEFT JOIN product_price AS PP ON P.product_id = PP.product_id 
LEFT JOIN tbl_customer_payment_history AS CPH 
ON CO.order_id = CPH.order_id WHERE CO.order_id = '60' 
GROUP BY CO.order_id, CO.created, C.company_name, CONCAT(C.delivery_address1, ', ', C.delivery_address2), COD.qty, P.product_name, PP.amount 

6 个答案:

答案 0 :(得分:0)

试试这个:在循环外定义$data

  $result = mysql_query($sql);
  $data = array(); // initialize/define the variable outside the loop
  while($row = mysql_fetch_array($result)) {
        $data[] = array(
            $row['Qty'], 
            $row['Description'] 
        );      
    }

答案 1 :(得分:0)

我在本地尝试使用我的数据库的代码工作正常

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$sql = "Select * from table_1";
$result = mysql_query($sql);
    while($row = mysql_fetch_array($result)) {
        $data[] = array(
            $row['name'], 
            $row['email'] 
        );      
    }

    print_r($data);

?>

答案 2 :(得分:0)

您的代码应该可以找到(假设您的查询是正确的)。

  • 你需要检查的是变量名称从查询中返回的是'qty'还是'Qty' 在php中是区分大小写的。

答案 3 :(得分:0)

我使用了以下代码并获得了理想的结果...... 试试吧


    $conn=mysql_connect("localhost", "root", "") or die (mysql_error ());
    mysql_select_db("TEST",$conn) or die(mysql_error());
    $strSQL = "SELECT * FROM product";
    $result = mysql_query($strSQL) or die(mysql_error());
        while($row = mysql_fetch_array($result)) {
            $data[] = array(
                $row['Qty'], 
                $row['Description'] 
            );      
        }

    print_r($data);

还尝试使用mysqli或pdo而不是mysql。

确保$ row ['Qty']中的列名称因此,如果php区分大小写,那么列应该是Qty not qty .....

或者你的查询有问题......

或数据库可能为空且未提取任何行

答案 4 :(得分:0)

看看: http://php.net/manual/en/function.mysql-fetch-array.php

试试这个:

- 在MYSQL_BOTH函数中添加参数mysql_fetch_array()(您也可以使用MYSQL_NUMMYSQL_ASSOC,查看链接)

- 以与表格中的字段名称完全相同的方式写入您的密钥(区分大小写)

$result = mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $data[] = array(
        $row['Qty'], 
        $row['Description'] 
    );      
}

答案 5 :(得分:0)

很抱歉这是一个严肃的挖掘,但这件事发生在我之前,我发现我正在关闭页面上的连接(来自不同的查询),甚至没有意识到!在新查询下方移动关闭连接后,它完全正常。

希望如果其他人遇到同样的问题,这可能会有所帮助!