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\bookshopesult.php on line 6 and defined in D:\xampp\htdocs\bookshop\classes\book_info.php on line 17
Here is my code Snippet:
<?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);
}
}
}
?>
<?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;
}
}
}
?>
<?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);
?>
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);