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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:58:41+00:00 2026-05-23T06:58:41+00:00

I’m a beginner in OOP PHP. I’m trying to make a class that will

  • 0

I’m a beginner in OOP PHP.
I’m trying to make a class that will connect,query and fetch data
I done the below coding

class MySQL {

    private $set_host;
    private $set_username;
    private $set_password;
    private $set_database;

     public function __Construct($set_host, $set_username, $set_password){
        $this->host = $set_host;
        $this->username = $set_username;
        $this->password = $set_password;
        $con= mysql_connect($this->host, $this->username, $this->password);
        if(!$con){ die("Couldn't connect"); }
    }


    public function Database($set_database)
    {   
        $this->database=$set_database;
        mysql_select_db($this->database)or die("cannot select Dataabase");
    }

    public function Fetch($set_table_name){
        $this->table_name=$set_table_name;
        $query=mysql_query("SELECT * FROM ".$this->table_name); 
    $result= mysql_fetch_array($query);
    }
}

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');

what I’m trying to achieve is this

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');

and I want to fetch the data using a format like this

echo $result[0];

but I’m not getting that logic to make this happen
please help

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-05-23T06:58:42+00:00Added an answer on May 23, 2026 at 6:58 am

    Your Fetch function is only pulling one row from the database, and you aren’t returning the results…

    The method isn’t the best, but in order to achieve what you’re trying to do:

    public function Fetch($set_table_name){
        $query=mysql_query("SELECT * FROM ".$set_table_name); 
        $result = array();
        while ($record = mysql_fetch_array($query)) {
             $result[] = $record;
        }
        return $result;
    }
    

    This will make each row a part of $result, but you’ll have to access it like this:

    You would call the fetch function like this:

    $result = $connect->Fetch('posts');
    
    echo $result[0]['columnName'];  // for row 0;
    

    Or in a loop:

    for ($x = 0; $x < count($result); $x++) {
       echo $result[$x][0] . "<BR>";  // outputs the first column from every row
    }
    

    That said, fetching the entire result set into memory is not a great idea (some would say it’s a very bad idea.)

    Edit: It also looks like you have other issues with your class… I have to run but will check back tomorrow and if others haven’t set you straight I will expand.

    Edit2: Ok, going to do a once-over on your class and try to explain a few things:

    class MySQL {
    
      //declaring the private variables that you will access through $this->variable;
      private $host;  
      private $username;
      private $password;
      private $database;
      private $conn;  // Adding the connection, more on this later.
    
      public function __Construct($set_host, $set_username, $set_password){
        $this->host = $set_host;
        $this->username = $set_username;
        $this->password = $set_password;
        // Combining the connection & connection check using 'or'
        // Notice that I removed the semi-colon after the mysql_connect function
        $this->conn = mysql_connect($this->host, $this->username, $this->password)
                      or die("Couldn't connect");
      }
    
      public function Database($set_database)
      {   
        $this->database=$set_database;
        // Adding the connection to the function allows you to have multiple 
        // different connections at the same time.  Without it, it would use
        // the most recent connection.
        mysql_select_db($this->database, $this->conn) or die("cannot select Dataabase");
      }
    
      public function Fetch($table_name){
        // Adding the connection to the function and return the result object instead
        return mysql_query("SELECT * FROM ".$table_name, $this->conn);         
      }
    
    }
    
    $connect = new MySQL('localhost','root','');
    $connect->Database('cms');
    $posts = $connect->Fetch('posts');
    
    if ($posts && mysql_num_rows($posts) > 0) {
         echo "Here is some post data:<BR>";
         while ($record = mysql_fetch_array($posts)) {
             echo $record[0];  // or a quoted string column name instead of numerical index.
         }
    } else {
         echo "No posts!";
    }
    

    I hope this helps… It should get you started at least. If you have more questions you should ask them separately.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
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
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.