function 안에 $sql 구문 넣기 되나요?
본문
function eee(){
$sql = "SELECT * FROM gml_write_free WHERE wr_5 = '$aatoday';
return $sql;
}
echo eee();
요러코롬 할려고 하는데 될까요?
!-->답변 3
네 상관없습니다.
하지만 저렇게 쿼리문장을 가져오기보다는 함수내에서 쿼리문을 실행하고 그 결과값을 가져와서 사용하는게 더 낫지 않을까요??
이유가 있으시다면 저게 더 낫습니다..
function eee(){
$sql = "SELECT * FROM gml_write_free WHERE wr_5 = '$aatoday';
$result = sql_query($sql);
$list = array();
for($i=0; $row=sql_fetch_array($result); $i++){
$list[$i] = $row;
}
return $list;
}
print_r2($list);
어떤결과를 원하시는지 모르겠지만 해당 구문을 사용하시면 $sql 질의 결과가 아닌 sql 구문 자체가 출력됩니다.
글로벌 변수로 선언되어있지 있거나 값을 전달받지 않는 이상 함수 내부에서 외부의 변수를 가져올 수 없습니다.
그리고 '{$aatoday}' 로 변경하셔야 변수가 제대로 들어갈 것 같구요
됩니다.
열고 닫기 잘 맞춰주고,
뜬금없이 등장하는 변수가 없도록 처리하면요.
// Functions
// - https://www.php.net/manual/en/language.functions.php
// - http://docs.php.net/manual/kr/language.functions.php
function eee($aatoday){
$sql = "SELECT * FROM gml_write_free WHERE wr_5 = '{$aatoday}'";
return $sql;
}
echo eee('값');
// Variable scope
// - https://www.php.net/manual/en/language.variables.scope.php
// - http://docs.php.net/manual/kr/language.variables.scope.php
function eee(){
global $aatoday;
$sql = "SELECT * FROM gml_write_free WHERE wr_5 = '{$aatoday}'";
return $sql;
}
echo eee();
// $GLOBALS
// - https://www.php.net/manual/en/reserved.variables.globals.php
function eee(){
$sql = "SELECT * FROM gml_write_free WHERE wr_5 = '{$GLOBALS['aatoday']}'";
return $sql;
}
echo eee();
답변을 작성하시기 전에 로그인 해주세요.