2

Ay,

I defined (in config.php):

$mysql = mysqli_connect (..);

for connecting to the mySQL database. now I want to use the variable in my functions (functions.php):

function x () { $fetch = ($mysql, "SELECT ...")); }

without using global:

function x () { global $mysql ... }

any clean ideas (procedural)?

j0hn
  • 57
  • 7

2 Answers2

1

You can e.g. pass the connection resource to your function

function x ($mysqli) { $mysqli->... }
VolkerK
  • 95,432
  • 20
  • 163
  • 226
1

You can access variables in global-scope without the global-keyword by using the Superglobal $GLOBALS:

function x() { $GLOBALS['mysql']->... }

Doc: http://php.net/manual/en/reserved.variables.globals.php

I would recommend to create a separate function that returns the Database-Handle/Object you can use instead of the variable. e.g.:

function db() { return $GLOBALS['mysql']; }
function x() { db()->... }
jpossi
  • 364
  • 2
  • 4