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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:14:14+00:00 2026-05-18T22:14:14+00:00

I am attempting to put all my database connections in 1 php file, rather

  • 0

I am attempting to put all my database connections in 1 php file, rather than in each of my individual php pages. I have the following:

//conn.php:

<?php

class conn {
   var $username = "name";
   var $password = "password";
   var $server = "localhost";
   var $port = "3306";
   var $databasename = "db";
   var $tablename = "tablename";
   var $connection;

   public function getConnected() {

       $this->connection = mysqli_connect(
       $this->server,
       $this->username,
       $this->password,
       $this->databasename,
       $this->port
        );
}


}
?>

// file.php:

<?php
require_once("conn.php");

class myClass{

public function con() {

$conn = new conn();
$conn->getConnected();    
}

public function myF() {
$stmt = mysqli_prepare($conn->connection, "SELECT * FROM $conn->tablename");

mysqli_stmt_execute($stmt);
    }
}
?>

I then call this as follows:

$myNew = new myClass();
$myNew->con();
$myNew->myF();

When I call this, I get the following error:

Undefined property: myClass::$connection

What am I doing wrong?

  • 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-18T22:14:15+00:00Added an answer on May 18, 2026 at 10:14 pm

    In myClass, you connect in con(), but $conn is a variable local to that method; i.e. the class itself does not have access to it. To fix this,

    class myClass {
    
        public $conn;
    
        public function con() {
            $this->conn = new conn();
            $this->conn->getConnected();    
        }
    
        public function myF() {
            $stmt = mysqli_prepare($this->conn->connection, "SELECT * FROM {$this->conn->tablename}");
            mysqli_stmt_execute($stmt);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error when attempting to connect to the MySQL database on
I have a problem... in attempting to parse a file seperated by nothing more
I'm attempting to put a php variable inside a javascript function and having no
I'm attempting to put together some basic report screens. I've got some fairly complicated
I'm attempting to put a few drop down menus inside of an a4j:repeat. The
I'm attempting to put together a little mashup with some twitter APIs. However, the
Here's a snippet of a script of mine attempting to put extra Unicode awesomeness
I am attempting to create a WrapPanel with seamless ImageButtons containing Artwork. I put
Here's the situation: I'm attempting to get a collection of all types in my
I'm attempting to create a 100% width toolbar. This toolbar needs to have a

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.