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 7039907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:51:17+00:00 2026-05-28T01:51:17+00:00

Thanks to a poster here at SO, I started using MySQLI and prepared statements.

  • 0

Thanks to a poster here at SO, I started using MySQLI and prepared statements. However, there is one issue I’m not sure how to adapt from the way I used MySQL.

Before, I would open a connection to MySQL at the beginning of my main PHP script. Then any sub-script or class I used would automatically use the same connection just by calling the mysql_query() call.

Now, with MySQLI, it seems that if I want to use the connection in a class, I have to either pass the connection through the constructor or create a new MySQLI connection.

Is there a way to use a MySQLI connection the same way I was using the MySQL connection before?

take care,
lee

  • 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-28T01:51:18+00:00Added an answer on May 28, 2026 at 1:51 am

    Passing the connection into the constructor is often the preferable method. That is called “Dependency Injection” and is useful for software testing.

    However, if you would prefer not to use that method, you can simply access the connection globally inside any class method:

    // MySQLi connection created in the global scope
    $conn = new MySQLi(...);
    
    class someClass {
      public someMethod() {
        // Call $conn with the global keyword
        global $conn;
    
        $conn->query(...);
      }
    }
    

    Better though, is to store $conn in a class property:

    class someClass {
      // Connection property
      public $conn;
    
      public function __construct($conn) {
        $this->conn = $conn;
      }
    
      public someMethod() {
        $this->conn->query(...);
      }
    }
    

    If you are using MySQLi in a procedural context rather than object-oriented, you will need to supply the link parameter. There really isn’t a way around it.

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

Sidebar

Related Questions

Long time lurker, first time poster here. My question is: Using C# 2.0, is
This is a follow-up question to the one I posted here (thanks to mario
There are many questions concerning this issue here, but I have tried the solutions
Thanks for answers,Actually I am not puzzled about draw 1024*768 pixels is slower than
thanks in advance for your help. I am wondering if there is a (design)
Thanks for reading this. I have markup similar to what is below. Using the
first poster here. Trying to use sed to find and replace a word. Code
So here's some code on http://groups.google.com/group/android-developers/browse_thread/thread/16a2216b39513674 in which the poster writes that he's shortened
first time poster - semi-newb to Rails. Here is my question. Suppose you have
I was reading a question posted here: Sudoku algorithm in C# And one of

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.