如何检索多行MYSQL [关闭]

$sql = "SELECT * FROM orders WHERE ordercode = '$oc' AND order_status = '$pendingorderstatus'";
        $sqlresult = mysql_query($sql);
        $pendingorderrow = mysql_fetch_array($sqlresult);

        print_r($pendingorderrow);

Guys, I have the code above to retrieve records from the database, but it will only retrieve 1 row of in array format. I have multiple row of same ordercode and pendingorderstatus in the database, why it doesn't retrieve all the records/rows?

You need to use a loop to iterate through the result set. A while loop is the easiest to use here:

while ($pendingorderrow = mysql_fetch_array($sqlresult)){
    print_r($pendingorderrow);
}

You need to loop through the resultset !

while($pendingorderrow = mysql_fetch_array($sqlresult))
{
$orderarray[]=$pendingorderrow['orders'];
}

print_r($orderarray);

Bottomline

This (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

because you need to iterate over the result set:

$sql = "SELECT * FROM orders WHERE ordercode = '$oc' AND order_status = '$pendingorderstatus'";
$sqlresult = mysql_query($sql);
while($pendingorderrow = mysql_fetch_array($sqlresult))
{
    //process here $pendingorderrow, e.g. print_r($pendingorderrow);
}

If you are a different strokes kind of person and don't want to iterate over the array, I might suggest some group_concat and explode action, mainly if you are doing only one column, but you could do it for multiple also.

$sql = "SELECT group_concat(orders.columnA) FROM orders WHERE ordercode = '$oc' AND order_status = '$pendingorderstatus'";

Just do the group_concat for each field you want to select from the table. Explode result, and voila array. Or just iterate over the result set...

Just figured throw that there in for completeness, you should be aware of all options when deciding which tool fits what need. The more you know.