简体   繁体   中英

Php function doesn't see mysqli connection

I can't use mysqli queries within php function without violating the DRY principle. Before my function I have following mysql configuration code:

//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']);

And my function looks like this:

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

I also tried to send connection as input variable like this:

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

Neither are working. The only working possibility I found is when I copy-paste my database configurations in my function, but it's not an option, because i need to execute queries in multiple functions. How can I fix it?

PS Bad, but working code:

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

Using the global keyword and changing the scope of your connection variable and including the database connection variable as a function parameter are both valid methods to accomplish this task.

But since both of those aren't working, it's possible that you may have closed a previously opened connection before the function call.

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

function writeLog($isError) <== Results in Error
function writeLog($isError, $connection) <== Results in Error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM