0

我在购物车中收到错误邮件“警告:mysql_fetch_assoc():提供的参数不是有效的 MySQL 结果资源”,我在邮件“总计:0.00 美元”上只收到此文本,但购物车不是空的

function emailcartview() {
$total=0;
$totalvalue=0;
$str = '';
foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_'){
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, name, price, item_number FROM products WHERE id='.mysql_real_escape_string($id));


            $cart = array();
            while ($get_row = mysql_fetch_assoc($get)) {
                $cart[] = array(
                'hsid' => $get_row['id'],
                'price' => $get_row['price'],
                $price = $get_row['price'],
                'itemno' => $get_row['item_number'],
                'name' => $get_row['name'],
                'value' => $value,
                $sub = $value*$cost,
                $total += $sub,
                $totalvalue = $value,
                'realsub' => number_format($sub, 2),
                );

                }
                foreach($cart as $index => $record){
                    global $emailcart;
                    $str = "Item: {$record['itemno']} - {$record['name']} | Qty: {$record['value']} | Cost: {$record['price']} x {$record['value']} = {$record['realsub']} <br>";

           }
        }
    }
}
$str = "Total : $".number_format($total, 2);
return $str;
 }
4

1 回答 1

0

可能mysql_query()由于 sql 错误而返回 FALSE。并且由于脚本没有错误处理,这个 FALSE 已被传递给mysql_fetch_assoc.

$get = mysql_query('SELECT id, name, price, item_number FROM products WHERE id='.mysql_real_escape_string($id));
if ( !$get ) {
    trigger_error(mysql_error(), E_USER_ERROR);
}
于 2013-03-14T12:15:04.770 回答