-1

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

Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • 1
    1) Check if your connection was successful or not 2) Add error reporting to the top of your file(s): `ini_set("display_errors", 1); error_reporting(E_ALL);` and tell us if you get any. 3) Also do you pass the connection to the function ? – Rizier123 Aug 31 '15 at 06:57
  • 1
    Where is `$aDB` supposed to come from here?! – deceze Aug 31 '15 at 07:02
  • 1
    You can't really *set up globals*. You can set variables outside of any function, and even call this a global, but this does not make the variable implicitly available inside functions/methods. Either you need to pass your variable around or use `global $...` inside the function (note that the second option is something you should avoid). – Yoshi Aug 31 '15 at 07:02
  • Oops @deceze tha twas a typo, that was my work around because I couldnt access it haha. – Noitidart Aug 31 '15 at 07:04
  • 1
    Then it's a duplicate of [Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?](http://stackoverflow.com/q/16959576/476) – deceze Aug 31 '15 at 07:06
  • Thanks @deceze can you please see my edit, I tried the `use` thing its telling me though `Parse error: syntax error, unexpected T_USE, expecting '{' ` – Noitidart Aug 31 '15 at 07:10
  • 1
    `use` is only for ***anonymous functions***. – deceze Aug 31 '15 at 07:11
  • Ah thanks @deceze so is there any way to do it without usign `global`? :( – Noitidart Aug 31 '15 at 07:13
  • 1
    As the duplicate says: *pass it as parameter*. If that gets too tedious, you'll want to start learning about classes and dependency injection. – deceze Aug 31 '15 at 07:14
  • Ah ok thanks much man. – Noitidart Aug 31 '15 at 07:21
  • @deceze i learned a ton from your comments thank you so much!! – Noitidart Sep 04 '15 at 16:05

1 Answers1

2

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.

Harshit
  • 5,147
  • 9
  • 46
  • 93