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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:34:50+00:00 2026-05-30T17:34:50+00:00

I started using PHP classes and looked into OOP. I played around by creating

  • 0

I started using PHP classes and looked into OOP.

I played around by creating a Database class that looks something like this (snippet):

class Database
{

private $link;
private $result;
private $count;

    function connect()
    {
        $this->link = mysql_connect(DB_HOST, DB_USER, DB_PW);
        $return = $this->link ? 'Connected to database.' : 'Failed to connect.';
        $this->open(); // I have another function in this class to select the db
        echo $return;
    }

    function query($query)
    {
        $this->result = mysql_query($query);

    }

    function fetch()
    {
        return mysql_fetch_assoc($this->result);

    }

    function count()
    {
        $this->count = mysql_num_rows($this->result);
        return $this->count;
    }
}

Just to test, I created another class called Guestbook that looks like this:

class Guestbook
{

    function fetch()
    {
        Database::query('SELECT * FROM guestbook LIMIT 2');
        while($row = Database::fetch()){
        $result_array[] = $row;

        return $result_array;
    }   

}

Now I tested calling the functions:

$db = new Database();
$db->connect();
$db->query('SELECT * FROM test');
while($row = $db->fetch()) {
    echo $row['id'].'<br />';
}
echo $db->count();
$db->close();

This works as expected. So I went on with the Guestbook class:

$db->connect();
$guestbook = new Guestbook();
$results = $guestbook->fetch();

foreach($results as $gb) {  
    echo $gb['id'];
}

echo $db->count(); // HERE'S MY PROBLEM !

$db->close();

Everything works as I wanted, but the second time I call $db->count() it echos the previous count and not 2 (as I set LIMIT 2 in the Guestbook fetch function).

How do I interact with these classes properly, so I can use something like $db->count() globally?

Thanks in advance for any hints or solutions!

  • 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-30T17:34:52+00:00Added an answer on May 30, 2026 at 5:34 pm

    It is hard to give you a definitive advice on how you could wrap your database accessor with OOP and still access the count() globally.

    Clearly your code is wrong.

    by using the notation

    Database::query
    

    you are referring to some kind of static methods on the Database class which does not seem to be the intent here. It looks like your code does not crash because of some backward compatibility with PHP4.

    you could rewrite Guestbook along this way :

    class Guestbook
    {
    
        private $db;
    
        function __construct(&$db) {
            $this->db = $db;
        }
    
        function fetch($option)
        {
            $this->db->query('SELECT * FROM guestbook LIMIT 2');
            while($row = $this->db->fetch()){
            $result_array[] = $row;
    
            return $result_array;
        }   
    
    }
    

    and then

    $db = new Database();
    $db->connect();
    $guestbook = new Guestbook($db);
    $results = $guestbook->fetch();
    echo $db->count();
    

    This technique uses a pattern known as “Dependency Injection”

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

Sidebar

Related Questions

I started using PHP a couple of months ago. For the sake of creating
I have one class user that is initiated in /update/user.php when update.php is started.
3 days ago I started rewriting one of my scripts in OOP using classes
I just started using php arrays (and php in general) I have a code
I just started using Netbeans to debug PHP apps, but when I inspect any
I've started to learn OO programming, but using the PHP language with the help
I started using NetBeans for Python development, and so far it looks pretty good.
I started using Core Data for iPhone development. I started out by creating a
I recently started C++. I am using PHP right now and decided to take
It's been around 5 months since I picked up a PHP book and started

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.