我有一個index.php文件是這樣的:PDO變量是在MySQL的函數未定義
require_once("../resources/config.php");
require_once(LIBRARY_PATH . "/mysql.php");
...
if(checkIfMailExists($email)) {
$error = true;
$errormsg = "This e-mail is already in use!";
}
mysql.php是:
try {
$pdo = new PDO('mysql:host=' . $config['db']['db1']['host'] . ';dbname=' . $config['db']['db1']['dbname'], $config['db']['db1']['username'], $config['db']['db1']['password']);
}catch(PDOException $e){
echo "Cant connect to mysql DB!";
}
function checkIfMailExists($email) {
$query = "SELECT * FROM users WHERE email = '".$email."'";
$num = $pdo->query($query);
if($num->rowCount() > 0){
return true;
}else{
return false;
}
}
而且它的出現,從功能$ PDO是未定義。即使我刪除try-catch。 我固定它的唯一方法 - 我發送$ pdo作爲函數的參數,但它似乎是錯誤的。任何建議傢伙?
[作用域範圍](http://php.net/manual/en/language.variables.scope.php)。 – aynber