Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:31:01+00:00 2026-05-24T01:31:01+00:00

I am trying to write a function that connects to a database and returns

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:31:02+00:00Added an answer on May 24, 2026 at 1:31 am

    I think it’s okay:

    function viewer ($view_type){  
        GLOBAL $dbc; // here is your database connection
        if ($view_type=1){
            $query="SELECT * FROM table WHERE the_id='1234'";
            $run= mysqli_query($dbc, $query) or die....;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a
I'm trying to write a function that is able to determine whether a string
I'm trying to write a regex function that will identify and replace a single
I am trying to write a JavaScript function that will return its first argument(function)
I'm trying to write a custom function that will let me retrieve a cell
I am trying to write my first real python function that does something real.
I am trying to write a static function to Or two expressions, but recieve
I'm trying to write a branchless function to return the MAX or MIN of
I'm trying to write a static member function in C# or find one in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.