Nick20 Posted June 27, 2012 Share Posted June 27, 2012 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 Nick Link to comment Share on other sites More sharing options...
Edward Posted June 28, 2012 Share Posted June 28, 2012 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'); 1 Link to comment Share on other sites More sharing options...
Larry Posted July 1, 2012 Share Posted July 1, 2012 It'd be better to pass the database connection to the function as a parameter. Link to comment Share on other sites More sharing options...
Recommended Posts