これは、Oops コンセプトに基づく PHP での最初のプロジェクトです。データベース テーブルからすべての Subject_details を取得しようとしていますが、どこが間違っているのかわかりません。
index.php ページを実行すると、次のようなエラーが表示されます。
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
ここに私のコードスニペットがあります:
接続.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 クラス
<?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);
?>