이 코드는PHP : 함수가 필요한 파일의 변수를 사용할 수없는 이유는 무엇입니까?
#DB Informations (username/password)
require('/path/to/db_informations.php');
# This function return the 'name of the $ID from the db
function getName($ID){
$link = new PDO($dsn, $user, $pwd);
$query = "SELECT `name` FROM `tables` WHERE `id`=$ID";
$smt = $link->prepare($query);
$smt->execute();
$name = $smt->fetch(PDO::FETCH_ASSOC);
return $name['name'];
}
# db_informations contain the credentials to connect the database. ($dsn, $user, $pwd)
MMH require(/path/to/db_informations.php')
나는`넣어) (요구하는 경우에도 함수 내에서 작동하지 않는 경우, 데이터베이스에서 선택된 $의 ID의 이름을 반환하도록되어있다; ' 기능. 내 실수를 이해하지 못한다. 나를 설명해 주시겠습니까?
/path/to/file이 PHP에 포함되지 않은 이유는 무엇입니까? 및 방법?
[가변 범위] (http://www.php.net/manual/en/language.variables.scope.php)에 대해 읽어 보시기 바랍니다. –
링크 Mark에 감사드립니다! – torr
변수를 함수로 구문 분석 한 다음 반환하십시오. – Vector