This is connection string
global $db_connection;
$db_connection = false;
$db_connection = mysqli_connect($config['server'], $config['username'], $config['password'], $config['database']);
Function:
function lastInsertId() {
return mysqli_insert_id($db_connection);
}
Getting error
mysqli_insert_id() expects parameter 1 to be mysqli, null given in ...
What could be the issue?
If you want to use a global variable for your mysql connection you must specify the variable with global
for it to be usable in a function:
function lastInsertId() {
global $db_connection;
return mysqli_insert_id($db_connection);
}
You need function like:
function lastInsertId($db_connection) {
return mysqli_insert_id($db_connection);
}
$db_connection = mysqli_connect($config['server'], $config['username'], $config['password'], $config['database']);
$id = lastInsertId($db_connection);
And when you are calling function pass connection object in argument.
EDITED : global function in php not working like they maybe works in other programming languages(eg. javascript). You must explicitly define global variable.
function lastInsertId($db_connection) {
global $db_connection;
return mysqli_insert_id($db_connection);
}
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.