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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:32:05+00:00 2026-06-13T00:32:05+00:00

This is my first project in PHP based on the Oops Concept. I am

  • 0

This is my first project in PHP based on the Oops Concept. I am trying to get all the Subject_details from a database table, but I do not see where i am making a mistake.

When I run my index.php page, I get an error saying:

Catchable fatal error: Argument 1 passed to book_info::__construct() must be an instance of connection, none given, called in D:\xampp\htdocs\bookshop\result.php on line 6 and defined in D:\xampp\htdocs\bookshop\classes\book_info.php on line 17

Here is my code Snippet:

connection.php

                <?php

            /*
             * To change this template, choose Tools | Templates
             * and open the template in the editor.
             */

            /**
             * Description of connection
             *
             * @author Ashutosh
             */
            class connection {
                //put your code here 
            private $host = 'localhost';
            private $dbname = 'bookfinder_com';
            private $username = 'bookfinde';
            private $password ='4324dsfs';  

            public $con = '';

            function __construct(){

                $this->connect();   

            }

            function connect(){

                try{

                    $this->con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username, $this->password);
                    $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);


                }catch(PDOException $e){

                    echo 'We\'re sorry but there was an error while trying to connect to the database';
                    file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);

                }
                   }   
            }

            ?>

book_info.php Class

            <?php

        /*
         * To change this template, choose Tools | Templates
         * and open the template in the editor.
         */

        /**
         * Description of account_info
         *
         * @author Ashutosh
         */
        class book_info{

          private $con;

            public function __construct(connection $con) {
                $this->con = $con->con;
            }


        function getSubjectInfo(){

            $sub_info = $this->con->prepare("SELECT * FROM subjectdetails");
            $sub_info->execute();

            $results = $sub_info->fetchAll(PDO::FETCH_OBJ);

            foreach ($results as $key) {
                $results->subject_name; 
            }
        }   
        }
        ?>

index.php

        <?php
        include_once 'classes/connection.php';
        include_once 'classes/book_info.php';

        $con = new connection();
        $info = new book_info();
        $info->getSubjectInfo();

        echo $info;
        print($info);
        ?>
  • 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-13T00:32:06+00:00Added an answer on June 13, 2026 at 12:32 am

    You have defined your connection, and the book_info constructor expects the connection as its parameter:

    This is the book_info constructor:

    // Constructor expects a parameter, class `connection`
    public function __construct(connection $con) {
         $this->con = $con->con;
    }
    

    But you have called the constructor with no parameter.

    $con = new connection();
    // Pass $con into the book_info constructor
    // The constructor expects one parameter of class `connection`
    $info = new book_info($con);
    

    You’ll then have a problem where you won’t see any output, because getSubjectInfo() doesn’t return anything. return the array from it.

       function getSubjectInfo(){
    
            $sub_info = $this->con->prepare("SELECT * FROM subjectdetails");
            $sub_info->execute();
    
            $results = $sub_info->fetchAll(PDO::FETCH_OBJ);
    
            foreach ($results as $key) {
                // Maybe you intend to echo here?
                // Use $key, not $results
                echo $key->subject_name; 
            }
            // Return the result array
            return $results;
        }  
    

    When calling it, store the result in a variable:

    $results = $info->getSubjectInfo();
    // Now it is available as output.
    print_r($results);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first android project so please help I am trying to create
This was my first Sinatra project - link shortener but I am stuck with
This is the first time I'm creating an open-source project, and I've decided (based
Imagine this as the code from build.xml: <project name=test project> <target name=first> <echo>first</echo> </target>
First of all, this is my first post in Stack Overflow and I'm trying
this is my first python project. I am having issues setting up a project
This is my first azure project and I'm not sure if I'm doing something
This is my first applet project and for some reason when I try to
This is my first time doing this sort of project so apologies if the
This is my first windows application. I have published my project and here are

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.