2014-09-20 111 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.")(不要忘记使用 “”)

希望这有助于..