2017-03-02 1 views
0

DRY 원칙을 위반하지 않고 PHP 함수 내에서 mysqli 쿼리를 사용할 수 없다. 제 기능을하기 전에 내가 MySQL을 구성 코드 다음 한 :php 함수가 mysqli 연결을 찾지 못한다

//database configuration 
$config['mysql_host'] = "myhost.eu-west-2.rds.amazonaws.com"; 
$config['mysql_user'] = "mylogin"; 
$config['mysql_pass'] = "mypassword"; 
$config['db_name'] = "mydbname"; 
$config['table_name'] = "mytablename"; 
$connection = mysqli_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass'], $config['db_name']); 

그리고 내 기능은 다음과 같습니다

function writeLog($isError, $connection) { 
    global $ipLong, $datetime, $procedure_index, $gotResults; 
    $sql = "INSERT INTO user_log VALUES (NULL, "; 
    $sql .= "\"$ipLong\", \"$datetime\", \"$procedure_index\", \"$gotResults\", \"$isError\""; 
    $sql .= ");"; 
    mysqli_query($connection, $sql); 
} 
: 나는이 같은 입력 변수로 연결을 보내려고

function writeLog($isError) { 
    global $connection, $ipLong, $datetime, $procedure_index, $gotResults; 
    $sql = "INSERT INTO user_log VALUES (NULL, "; 
    $sql .= "\"$ipLong\", \"$datetime\", \"$procedure_index\", \"$gotResults\", \"$isError\""; 
    $sql .= ");"; 
    mysqli_query($connection, $sql); 
} 

어느 쪽도 효과가 없습니다. 내가 찾은 유일한 작업 가능성은 필자의 데이터베이스 구성을 복사하여 내 함수에 붙여 넣을 때이지만 여러 옵션에서 쿼리를 실행해야하기 때문에 옵션이 아닙니다. 어떻게 해결할 수 있습니까?

P. 나쁜,하지만 작업 코드 :

//database configuration 
$config['mysql_host'] = "myhost.eu-west-2.rds.amazonaws.com"; 
$config['mysql_user'] = "mylogin"; 
$config['mysql_pass'] = "mypassword"; 
$config['db_name'] = "mydbname"; 
$config['table_name'] = "mytablename"; 
$connection = mysqli_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass'], $config['db_name']); 

function writeLog($isError) { 
    //database configuration, again. totally violating DRY principle. 
    $config['mysql_host'] = "myhost.eu-west-2.rds.amazonaws.com"; 
    $config['mysql_user'] = "mylogin"; 
    $config['mysql_pass'] = "mypassword"; 
    $config['db_name'] = "mydbname"; 
    $config['table_name'] = "mytablename"; 
    $connection = mysqli_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass'], $config['db_name']); 

    global $ipLong, $datetime, $procedure_index, $gotResults; 
    $sql = "INSERT INTO user_log VALUES (NULL, "; 
    $sql .= "\"$ipLong\", \"$datetime\", \"$procedure_index\", \"$gotResults\", \"$isError\""; 
    $sql .= ");"; 
    mysqli_query($connection, $sql); 
} 
+0

** 경고 ** : 사용한다 mysqli''사용하는 경우 [매개 변수가있는 쿼리 (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)와 [ 'bind_param'] (http://php.net/manual/en/mysqli-stmt.bind-param.php)을 사용하여 쿼리에 사용자 데이터를 추가하십시오. ** 심각한 [SQL injection bug] (http://bobby-tables.com/)를 만들었 기 때문에 문자열 보간이나 연결을 사용하여 **이를 수행하지 마십시오. **'$ _POST','$ _GET' 또는 ** 모든 ** 사용자 데이터를 쿼리에 직접 넣지 마십시오. 실수로 누군가를 악용하려는 경우 매우 위험 할 수 있습니다. – tadman

+0

팁 : 작게 시작하십시오. 쌓아 라. 예를 들어 함수에 큰 쿼리를 하드 코딩하지 않는 등 모듈화 된 방식을 유지하십시오. 자주 시험하고 무언가가 고장 났을 때주의를 기울이십시오. 버전 제어를 사용하여 결함을 식별하십시오. – tadman

+0

"보이지 않음"을 정의하십시오. 코드 색맹이거나 선글라스를 착용하고 있습니까? –

답변

0

함수의 매개 변수로 데이터베이스 연결 변수를 포함하여 global 키워드를 사용하여 연결 변수의 범위를 변경하고이 작업을 수행하기 위해 모두 유효한 방법이 있습니다.

하지만 둘 다 작동하지 않으므로 함수 호출 전에 이전에 연 연결을 닫은 것이 가능할 수 있습니다.

mysqli_close($connection); <== Closed connection. 

function writeLog($isError) <== Results in Error 
function writeLog($isError, $connection) <== Results in Error 
관련 문제