2014-09-20 107 views
2

當我試圖在PHP中執行一個函數時,MySQLi查詢正在執行,但只返回值0,但查詢在數據庫上執行。MYSQLi返回0然而查詢在數據庫上執行

function get_delivery_cost($mysqli, $item_ids) { 
    $query = $mysqli->prepare("SELECT (max(item_single_cost) + sum(item_additional_cost)) AS total FROM item_delivery WHERE item_id IN (?) AND item_single_cost NOT IN (SELECT max(item_single_cost) FROM item_delivery WHERE item_id IN (?)) LIMIT 1"); 
    $query->bind_param('ss', $item_ids, $item_ids); 
    $query->execute(); 
    $query->store_result(); 
    $query->bind_result($delivery_cost); 
    $query->fetch(); 
    return $delivery_cost; 
} 

的$ ITEM_IDS樣品是1,2,3,當相同的查詢被直接跑的總列返回對數據庫£3.30然而,庫MySQLi函數返回0

運行在PHP該功能是:

<? 
    array_push($item_ids, $basket_item->item_id); 
    $ids = join(',', $item_ids); 
    $delivery_cost = get_delivery_cost($mysqli, $ids); 
    echo number_format($delivery_cost, 2); 
?> 
+0

注意:你也應該叫'$查詢 - >關閉()'大功告成了。 – GolezTrol 2014-09-20 10:46:39

+0

由於您正在讀取數據,因此對'$ query-> store_result();的調用似乎已過時。不過,應該不重要。 – GolezTrol 2014-09-20 10:51:08

+0

您是否檢查過任何結果值?例如,你是否檢查'execute'和'fetch'是否返回了「false」以外的值? – GolezTrol 2014-09-20 10:52:36

回答

1

我敢肯定,問題是條件的這部分:

item_id IN (?) 

如果$ids$ is set to '1,2,3', then this is equivalent to item_id ='1,2,3'and not to item_id in(1,2,3)`。相反,您可以直接替換值:

item_id in (".$ids.") 
0

您需要使用IN操作來替代在$ids其中存儲的值。

這將是item_id in (".$ids.")(不要忘記使用 「」)

希望這有助於..