php 创建数据库连接类 (PDO) 并获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13240500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:01:29  来源:igfitidea点击:

Creating a database connection class (PDO) and fetch data

php

提问by black_belt

I am new to OOP, so I am trying to learn how to create classes and use them. Currently I am trying to fetch data from my MySQL table.

我是 OOP 的新手,所以我正在尝试学习如何创建类并使用它们。目前我正在尝试从我的 MySQL 表中获取数据。

To create the connection with MySQL I am using PDO. I have created a separate class for database connection. I have included the class in my show.php file. Now I want to fetch data from MySQL database. The problem is when I run my show.php file it shows this error message

要创建与 MySQL 的连接,我正在使用 PDO。我为数据库连接创建了一个单独的类。我已经在我的 show.php 文件中包含了这个类。现在我想从 MySQL 数据库中获取数据。问题是当我运行我的 show.php 文件时它会显示此错误消息

Fatal error: Call to undefined method DBConnection::prepare() in C:\xampp\htdocs\jm\show.php on line 10`

致命错误:调用 C:\xampp\htdocs\jm\show.php 中第 10 行的未定义方法 DBConnection::prepare()`

But it was supposed to display just array.

但它应该只显示array.

What is the solution to this problem?

这个问题的解决方案是什么?

File db.class.php

文件db.class.php

<?php
    class DBConnection {

        function DBConnection(){

            $host = 'localhost';
            $dbname = 'srijon';
            $user = 'root';
            $pass = '';

            try {
                $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
                $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                return $DBH;
            }
            catch(PDOException $e) {

                echo 'ERROR: ' . $e->getMessage();
            }

        } // function ends

    } // class ends
?>

File show.php

文件show.php

<?php
    require_once 'db.class.php';

    function get_all(){

        $db = new DBConnection();

        $sql = "SELECT * FROM information";
        $STH = $db->prepare($sql);
        $STH->execute();
        $STH->setFetchMode(PDO::FETCH_ASSOC);

        return $STH;
    }

    echo get_all();
?>

回答by PeeHaa

IMHO you can just inject the PDO connection into the functions that need it:

恕我直言,您可以将 PDO 连接注入到需要它的函数中:

<?php

$dbHandle = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$dbHandle->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// always disable emulated prepared statement when using the MySQL driver
$dbHandle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

function get_all($dbHandle) {
    $sql = "SELECT * FROM information";
    $stmt = $dbHandle->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);

    return $stmt;
}

get_all($dbHandle);

If you really think you need some class to access to database (other than PDO) (which you don't need) you would have to extend PDO:

如果您真的认为您需要某个类来访问数据库(PDO 除外)(您不需要),则必须扩展 PDO:

<?php

class DBConnection extends PDO
{
    public function __construct()
    {
        parent::__construct("mysql:host=$host;dbname=$dbname", $user, $pass);
        $this->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        // always disable emulated prepared statement when using the MySQL driver
        $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
}

$dbHandle = new DBConnection();

function get_all($dbHandle) {
    $sql  = "SELECT * FROM information";
    $stmt = $dbHandle->prepare($sql);
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_ASSOC);

    return $stmt;
}

get_all($dbHandle);

回答by Prash

Maybe change the DBConnections FUNCTION to a __contstruct() function. In addition, you'd need to extend the PDO class to use all the methods within it.

也许将 DBConnections FUNCTION 更改为 __contstruct() 函数。此外,您需要扩展 PDO 类以使用其中的所有方法。