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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:07:27+00:00 2026-05-16T22:07:27+00:00

I’m new to OOP and i’m trying to get a grasp on it. What

  • 0

I’m new to OOP and i’m trying to get a grasp on it. What i’m trying to do is return all of the data in the table. There is only 2 rows right now, but i’m not sure how to put all the data in an array and then return it all the way back to the $results variable.

Please help.

<?php
include('dates.php');

$events = new dates();
echo $events->getNumberEvents(); <-----returns the number of rows in the table (Works as expected)

$results = $events->getRows(); <------Doesn't work.

?>

Dates Class

<?php
/********************************************
* Class to connect and get dates from a database 
*********************************************/
require 'phpDatabaseClass.php';

class dates{

    private $db;
    private $arr2 = array();

    /****************************
    * Constructor which creates the $db variable
    ****************************/
    function __construct() 
    {
        $this->db = new phpDatabaseClass();
    }   

    /****************************
    * Function which calls the database class and returns the number of rows
    ****************************/
    public function getNumberEvents()
    {   
        $sql = "select count(*) from events";           
        return $this->db->queryNumber($sql);
    }

    /****************************
    * Function which calls the database class and returns the actual rows
    ****************************/
    public function getRows() 
    {
        $sql = "select * from events";

        $this->arr2 = $this->db->queryString($sql);

        foreach($this->arr2 as $key=>$val)
        {
            //$this->arr2 = $val;
            //echo $val;    
        }

    }
}
?>

phpDatabaseClass

<?php
/********************************************
* Class to connect and query a mysql database 
*********************************************/
require 'config.php';

class phpDatabaseClass {

    private $db;
    private $arr = array();

    /****************************
    * Constructor function which makes the connection to the database
    ****************************/
    public function __construct()
    {
        try 
        {
            $this->db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD);
        }
        catch(PDOexception $e)
        {
            echo 'Can\'t connect to database. Please contact the site administrator';
        }
    }

    /****************************
    * Function which returns the number of rows
    ****************************/
    public function queryNumber($sql)
    {                           
        $stmt = $this->db->prepare($sql);
        $stmt->execute();
        $resultsCount = $stmt->fetchColumn();

        return $resultsCount;
    }


    /****************************
    * Function which returns the data in the rows
    ****************************/
    public function queryString($sql)
    {                           
        $stmt = $this->db->prepare($sql);

        $stmt->execute();

        $results = $stmt->fetch(PDO::FETCH_ASSOC);

        foreach($results as $key=>$val)
        {
            $this->arr = $val;
            //echo $this->arr;
        }

        return array($this->arr);
    }
}
?>
  • 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-16T22:07:28+00:00Added an answer on May 16, 2026 at 10:07 pm
    public function getRows() 
    {
        $sql = "select * from events";
    
        $this->arr2 = $this->db->queryString($sql);
        $return = array();
        foreach($this->arr2 as $key=>$val)
        {
            $return[$key] = $val;  
        }
        return $return;
    }
    

    That is if you wish to do anything at that stage. To simply return you can do:

    return $this->db->queryString($sql);
    

    To echo do:

    foreach($results as $result)
    {
    
        echo $result->fieldname;
    
    }
    

    To echo everything do:

    foreach($results as $result)
    {
        foreach($result as $key => $value)
        {
            echo $key.': '.$value;
        }
    
    }
    

    The reason for accessing as an object and not an array is that mysqli returns rows as objects.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.