Jump to content
Larry Ullman's Book Forums

Using Global Variable To Pass Database Connection Into Function


Recommended Posts

Hi,

 

is it secure to pass a database connection into a function in an external file using a global variable. For example:

 

 

function get_all_products(){

global $dbc; //where $dbc is database connection

$q = 'SELECT * FROM product WHERE active="yes" ORDER BY CatId';

$r = MYSQLI_QUERY ($dbc, $q);

$data = array();

(etc)

 

 

or is there a better/safer way?

 

Thanks very much :rolleyes:

 

Nick

Link to comment
Share on other sites

Its better to avoid the use of global variables if possible. In Larry's book he uses the PHP require function to include a MySQL connection script, that way you can add the script in where ever you need it.

 

For example:

 

require ('mysqli_connect.php');

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...