如果我这样做
include('orderConst_pdo_connect.php');
switch ($type){
case "retrieve":
retrieve_address($customer_id);
break;
case "new":
new_address($customer_id, $address);
break;
case "update":
update_address($customer_id, $address_id);
break;
case "delete":
delete_address($customer_id, $address_id);
break;
}
function retrieve_address($customer_id){
$sth = $dbh->prepare("
SELECT *
FROM address
WHERE customer_id = :customer_id
ORDER BY CASE
WHEN use_frequency IS NULL THEN id
ELSE use_frequency
END ASC");
....
}
我收到错误
Fatal error: Call to a member function prepare() on a non-object
直到我把include('orderConst_pdo_connect.php')
放在函数中。有没有办法在页面顶部包含文件并让函数访问它,或者我是否需要将它包含在每个使用它的函数中?
答案 0 :(得分:3)
默认情况下,在包含文件中定义的变量在函数内部不可用。您可以使用global $dbh
答案 1 :(得分:1)
而不是使用global
,只需执行:
function retrieve_address($dbh, $customer_id) {
// blah
}