I am trying to write a function that connects to a database and returns some data to display, I have:
function viewer ($view_type){
if ($view_type=1){
$query="SELECT * FROM table WHERE the_id='1234'";
$run= mysqli_query($dbc, $query) or die....;
Which is called in:
include('/../includes/connect.php');
$view= 1;
viewer($view);
This is where I get the undefined variable dbc error. The $dbc variable is defined in connect.php which is called in the include(). I realise that the function cannot call the include() variable.
I have done some research and have found that I can call it as a global variable. However I am cautious of this as the dbc variable contains sensitive DB information:
$dbc=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
I have also tried adding the include to the function itself however as I need to connect to the DB on the page before calling the function i recieve ‘variable DB_HOST already defined’ errors.
Would it be appropriate to define $dbc as a global variable? If not how can I get around this?
I think it’s okay: