2017-03-02 80 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) { 
    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); 
} 

我還嘗試發送連接,這樣的輸入變量:

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); 
} 

兩個都沒有工作。我發現唯一的工作可能性是當我在我的函數中複製粘貼數據庫配置時,但它不是一個選項,因爲我需要在多個函數中執行查詢。我該如何解決它?

P.S.壞,但工作代碼:

//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

** WARNING **:當使用'mysqli'你應該使用[參數化查詢(http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[ 'bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](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