I have an index.php which is like:
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 is:
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;
}
}
And it appears, that $pdo from function is undefined. Even if i remove try-catch.
The only way i fixed it - i sent $pdo as a parameter to function, but it seems to be wrong. Any suggestions guys?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…