无法使用pdo方法显示db中的数据

时间:2012-10-14 12:56:31

标签: php mysql pdo

这是我在PHP中基于Oops概念的第一个项目。我试图从数据库表中获取所有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

这是我的代码片段:

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班级

            <?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 个答案:

答案 0 :(得分:0)

您已定义了连接,book_info构造函数需要将连接作为其参数:

这是book_info构造函数:

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

但是你没有参数调用构造函数。

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

然后你会遇到一个问题,你不会看到任何输出,因为getSubjectInfo()没有返回任何内容。 return来自它的数组。

   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;
    }  

调用它时,将结果存储在变量中:

$results = $info->getSubjectInfo();
// Now it is available as output.
print_r($results);