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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:22:31+00:00 2026-05-27T02:22:31+00:00

I’m trying to develop a system using MySQL and PHP. I’m using mysqli* function

  • 0

I’m trying to develop a system using MySQL and PHP. I’m using mysqli* function using OOP. The problems I’m having is the next one:

I have a class for connect to the database:

<?php
  class Conexion{
   $this->linkDb = new mysqli("127.0.0.1", "user", "pass");
   if($this->linkDb){
     $this->linkDb->select_db("test");
     return TRUE;
   }
  else{
     return FALSE;
  }
 } // End of class Conexion

?>

I have several classes to retrieve and store data in my database, for example:

<?php
class Product{

  public function listProducts()
  {
    $query = "SELECT * FROM products";
    $rsProducts = $linkDb->query($query);
    if($rsProducts){
       // ....
    }
    else{
      // ....
    }
  }
}
?>

And I use this classes in the next file (main):

<?php
  function __autoload($className)
  {
    require_once("./class/$className.php");
  }

  $cnx = new Conexion();
  $prd = new Products();
  if($prd->listProducts()){
    echo "OK";
  }
  else{
    echo "Wrong...";
  }
?>

When I try to run this file, I get the next error:
Fatal error: Call to a member function query() on a non-object in /home/emco/training/phpmysqli/class/Products.php on line 8

I know the error is because of the scope of $linkDb in the method listProducts(). My question is, How can I do to make the link of the conexion available in all the functions of my classes? Do I have to make a conexion link in each of the functions of my classes? What’s the correct way to do 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-27T02:22:32+00:00Added an answer on May 27, 2026 at 2:22 am

    You can either pass the $cnx object to each method call of Products, or establish it as a property of Products. (A third method would be to declare global $linkDb inside methods, but it is clearer to use a dependency injection.)

    Example 1: Inject the connection into the constructor and keep it as a property to Products

    class Product{
      // Database as property
      public $linkDb;
    
      // Pass it into the constructor
      public function __construct($cnx) {
        $this->linkDb = $cnx;
        // ...other stuff...
      }
    
      public function listProducts()
      {
        $query = "SELECT * FROM products";
        // Access it with $this->linkDb
        $rsProducts = $this->linkDb->query($query);
        if($rsProducts){
           // ....
        }
        else{
          // ....
        }
      }
    }
    
    // Use it as:
    $cnx = new Conexion();
    $prd = new Products($cnx);
    

    Example 2: Pass $cnx to each method call of Products where it is needed

    // Require one parameter $linkDb
    public function listProducts($linkDb)
    {
      $query = "SELECT * FROM products";
      $rsProducts = $linkDb->query($query);
      if($rsProducts){
       // ....
      }
      else{
        // ....
      }
    }
    
    // Call as
    $cnx = new Conexion();
    $prd = new Products();
    
    // Pass $cnx as a parameter
    if($prd->listProducts($cnx)){
      echo "OK";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to loop through a bunch of documents I have to put
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.