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

  • SEARCH
  • Home
  • 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 8229481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:48:35+00:00 2026-06-07T16:48:35+00:00

Below is my code under db.php <?php $con = mysqli_connect(‘localhost’, ‘root’, ”, ‘mydb’); /*

  • 0

Below is my code under db.php

    <?php
$con = mysqli_connect('localhost', 'root', '', 'mydb');

/* check connection */
if (!$con) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>

Under index.php I include db.php and functions.php as:

include("includes/db.php");
include("includes/functions.php");

functions.php is also using the db.php connection. I don’t have problem before when using mysql. But after I change mysql to mysqli I got an error “Warning: mysqli_query() expects parameter 1 to be mysqli, null given in” in my functions.php.

This is the function under functions.php that has an error:

function get_type($r_id){
    $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
    $row=mysqli_fetch_assoc($result);
    return $row['type'];
}

My solution is to add db.php in every function under functions.php that calls mysqli like:

function get_type($r_id){
    include("includes/db.php");
    $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
    $row=mysqli_fetch_assoc($result);
    return $row['type'];
}

I am wondering if this is the correct solution.

  • 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-06-07T16:48:37+00:00Added an answer on June 7, 2026 at 4:48 pm

    The problem is that $con is not available to your functions

    You can add another argument to each function

    function get_type($con, $r_id)...
    

    and then pass $con into it

    include('includes/db.php');
    include('includes/functions.php');
    $blah = get_type($con, 5);
    

    OR

    You can make $con accessible to each function by adding this global $con; for example

    function get_type($r_id){
        global $con;
        $result=mysqli_query($con, "select type from rooms where id=$r_id") or die("select type from rooms where id=$r_id"."<br/><br/>".mysqli_error());
        $row=mysqli_fetch_assoc($result);
        return $row['type'];
    }
    

    The choice my friend.. is yours

    (there may be other ways to skin this cat)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Php page the code for which is below, I am trying
I have the code below under Themes\Generic.xaml Now I need to get cPresenter in
Can someone convert below code to ASP format? <?php $data = ' [ {
In the below code there's a loop that features all products under a given
I have code that overloads operator new . The code below works fine under
I have a bash code below under FreeBSD: tar -c file1 > file1.tar It
I've found this odd case of some code (below) doesn't compile under Visual Studio
My current PHP code is working and styling my Theme Options page (located under
I am writing a simple file uploader in CodeIgniter 2.0.2. Pasting code below. Under
Below code not work, but it's work fine for jsf1.2. Now the framework is

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.