dongmei2957 2012-10-14 12:56
浏览 40
已采纳

无法使用pdo方法显示来自db的数据[关闭]

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:

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条回答 默认 最新

  • dongtaijiao7140 2012-10-14 13:01
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。