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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:13:33+00:00 2026-06-13T03:13:33+00:00

I’m trying to run a query to a MySQL database from within a class

  • 0

I’m trying to run a query to a MySQL database from within a class and it’s not working for some reason. I have the class in a separate file which I’m linking to with the require_once() function.

here’s what the main .php file looks like:

<?php
  require_once("connect.php");
  require_once("theClass.php");

  $a = new theClass;
  $a->runQuery();
}

connect.php:

<?php
//connect to mySQL database
$mysqli = new mysqli("host", "user", "password", "db");
if ($mysqli->connect_errno)
{
    echo "<br><h1>Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error . "</h1><br>";
}

theClass.php:

<?php
require_once('connect.php');

class theClass
{
  //class variables and other functions here

  function runQuery()
  {
    $query = "SELECT col_1 FROM db.table";
    $stmt = $mysqli->prepare($query);
    stmt->execute();
    $stmt->bind_result($r);

    while($stmt->fetch())
    {
      echo $r . "<br>";
    }
  }
};

I’ve tried copying the class into the main .php file and it still doesn’t work; however, I have used the exact same code( the query, prepare, execute, bind_result, and fetch part) in an external .php file as well as inside the main .php file and it has worked both times. This leads me to believe that you’re not able to run queries from inside a class or that there is a different way of going about doing so. Could anyone point me in the right direction?

Thanks

  • 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-13T03:13:34+00:00Added an answer on June 13, 2026 at 3:13 am

    Pass it to the method itself

    You have to pass the database object to the method, because they are not in the same scope:

    function runQuery($mysqli)
    

    and call it like

    $a = new theClass;
    $a->runQuery($mysqli);
    

    Pass it to the constructor

    If your class makes a lot of database calls, you could simply pass it in the constructor and save it as a private variable for later use:

    class theClass
    {
      private $mysqli;
    
      function __construct($mysqli) {
        $this->mysqli = $mysqli;
      }
    
      function runQuery()
      {
        $query = "SELECT col_1 FROM db.table";
        $stmt = $this->mysqli->prepare($query);
        stmt->execute();
        $stmt->bind_result($r);
    
        while($stmt->fetch())
        {
          echo $r . "<br>";
        }
      }
    };
    

    and call it like

    $a = new theClass($mysqli);
    $a->runQuery();
    

    Both methods make it clear that the dependency of your class is a mysqli object, which is good for future maintenance and readability.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a view passing on information from a database: def serve_article(request, id): served_article
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.