简体   繁体   中英

PHP global not available in function

I set up my global like this:

require('../scripts/mysql_db.php');
$DB = new mysql_db();
$connectid = $DB->sql_connect($mysql_host, $mysql_user , $mysql_password, $mysql_database);

I then use $DB throughout like this:

$query1 = $DB->query('SELECT ....');

However I wrote a function to use $DB and its not accessible for some reason:

function deletePendingRow($aOkReason, $aFailReason) {
    $query99 = $DB->query('DELETE .....');
}

I think this is a basic php thing, can someone help me understand why.

The mysql_db is here: https://github.com/Noitidart/MailtoWebmails-Backend/blob/master/scripts/mysql_db.php

Thanks

edit: i tried this:

function deletePendingRow($aOkReason, $aFailReason) use ($DB, $rowPending) {

It doesnt seem to work it tells me Parse error: syntax error, unexpected T_USE, expecting '{'

edit 2:

i also tried this:

$deletePendingRow = function($aOkReason, $aFailReason) use ($DB, $rowPending) {

};

but this throws Parse error: syntax error, unexpected T_FUNCTION in /home/a1304271/public_html/ajax/approve_pending.php

define it like to access global variables in the functions

global $DB;

Although if you are getting the database variable via parameters, then there should be no problem accessing it.

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