2017-03-02 52 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