【问题标题】:Object of class Database could not be converted to string类数据库的对象无法转换为字符串
【发布时间】:2023-04-01 08:44:02
【问题描述】:

我正在使用这个方法从数据库中获取数据

class Database {
    private static $_instance = null;
    private $_pdo,
            $_query,
            $_error = false,
            $_results,
            $_count = 0;

    private function __construct() {
        try {
            $this->_pdo = new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
        } catch (PDOException $e) {
            die($e->getMessage());
        }
    }

    public static function getInstance() {
        if (!isset(self::$_instance)) {
            self::$_instance = new Database();
        }
        return self::$_instance;
    }

    public function query($sql, $params = array()) {
        $this->_error = false;
        if ($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if (count($params)) {
                foreach ($params as $param) {
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }

            if ($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count   = $this->_query->rowCount();
            } else {
                $this->_error = true;
            }
        }

        return $this;
    }

    public function action($action, $table, $where = array()) {
        if (count($where) === 3) {
            $operators = array('=','>','<','>=','<=','<>');

            $field      = $where[0];
            $operator   = $where[1];
            $value      = $where[2];

            if (in_array($operator, $operators)) {
                $sql = "{$action} FROM {$table} WHERE ${field} {$operator} ?";
                if (!$this->query($sql, array($value))->error()) {
                    return $this;
                }
            }
        }
        return false;
    }

    public function get($table, $where) {
        return $this->action('SELECT *', $table, $where); 
    }

    public function delete($table, $where) {
        return $this->action('DELETE', $table, $where);
    }

    public function insert($table, $fields = array()) {
        if (count($fields)) {
            $keys   = array_keys($fields);
            $values = null;
            $x      = 1;

            foreach ($fields as $field) {
                $values .= '?';
                if ($x<count($fields)) {
                    $values .= ', ';
                }
                $x++;
            }

            $sql = "INSERT INTO {$table} (`".implode('`,`', $keys)."`) VALUES({$values})";

            if (!$this->query($sql, $fields)->error()) {
                return true;
            }
        }
        return false;
    }

    public function update($table, $id, $fields = array()) {
        $set    = '';
        $x      = 1;

        foreach ($fields as $name => $value) {
            $set .= "{$name} = ?";
            if ($x<count($fields)) {
                $set .= ', ';
            }
            $x++;
        }

        $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";

        if (!$this->query($sql, $fields)->error()) {
            return true;
        }
        return false;
    }

    public function results() {
        return $this->_results;
    }

    public function first() {
        return $this->_results[0];
    }

    public function error() {
        return $this->_error;
    }

    public function count() {
        return $this->_count;
    }
}

this is what i want to get this is my table

这是我的代码...

public function getModule() {
    $epreuve = $this->_db->get('epreuve', array('concour_code', '=', $this->data()->concour_code));
    foreach($epreuve->results() as $epreuve){
        echo "<tr><td>".$epreuve->designation."</td>"
        .$module = $this->_db->get('module', array('epreuve_code', '=', $epreuve->code ));               
        foreach($module->results() as $module){
        echo "<tr><td>".$epreuve->designation."</td>";
        }
        "</tr>";

    }
}

但我有这个错误

'' 可捕获的致命错误:无法将类数据库的对象转换为字符串''

【问题讨论】:

  • 你的数据库连接怎么样?
  • 尽可能显示整个数据库类,或者至少显示您在上面调用的函数/方法,例如 results()
  • 我添加了关于主题的完整课程:)
  • 我知道我可以使用此查询 $note = Database::getInstance()->query("SELECT ..... FROM ....WHERE .... ...“);但我想用 get() 方法得到结果

标签: php mysql


【解决方案1】:

错误似乎在这里:

echo "<tr><td>".$epreuve->designation."</td>"
    .$module = $this->_db->get('module', array('epreuve_code', '=',

请注意,您没有用分号关闭echo,并且在$module 之前有一个点,因此PHP 正在尝试使用$module 类加上迭代串连串连内。你不能那样做。

执行以下操作:

public function getModule() {
    $epreuve = $this->_db->get('epreuve', array('concour_code', '=', $this->data()->concour_code));

    foreach($epreuve->results() as $epreuve){
        echo "<tr>";
        echo "<td>".$epreuve->designation."</td>";
        $module = $this->_db->get('module', array('epreuve_code', '=', $epreuve->code ));              
        foreach($module->results() as $module){
            echo "<td>".$epreuve->designation."</td>";
        }
        echo "</tr>";
    }

}

建议:

在你的代码上

foreach($epreuve->results() as $epreuve){

foreach($module->results() as $module){

您不应该使用与您正在迭代的变量名称相同的变量名。试试改成

public function getModule() {
    $epreuve = $this->_db->get('epreuve', array('concour_code', '=', $this->data()->concour_code));

    foreach($epreuve->results() as $epreu){
        echo "<tr>";
        echo "<td>".$epreu->designation."</td>";
        $module = $this->_db->get('module', array('epreuve_code', '=', $epreu->code ));              
        foreach($module->results() as $mod){
            echo "<td>".$epreu->designation."</td>";
        }
        echo "</tr>";
    }
}

注意:HTML 表格有点混乱,我尽力理解它。根据您的需要进行更改。

【讨论】:

  • 谢谢它的工作...如果我想添加另一个条件例如 $module = $this->_db->get('note_condidat', array('module_code', ' =', .$epreu->code.'AND' .$module->condidat_concour_id. '=' $this->data()->ID.));
  • 记得把答案标记为正确答案。对于你的问题,试试这个gist.github.com/falmar/b92b29684c297169f62026c827d66637
  • 我有这个错误 她的致命错误:调用成员函数 results() 注意:代码是 $module = $this->_db->get('note_condidat', array( array( 'module_code', '=', $epreu->code, 'AND'), array('condidat_concour_id', '=', $this->data()->ID) ));所以我试图让用户 ID 与 condidat_condour_id 进行比较
  • condidat_concour_id 是否属于 module_code?用户 ID 在哪里?确保 $this->data()->ID 返回你想要的
  • 它和 $this->data()->concour_code 一样(从用户表中获取 concour_code)只是我这次想获取 ID ...
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 2015-07-27
  • 2012-04-29
  • 2011-04-06
  • 2011-11-30
  • 2011-08-30
相关资源
最近更新 更多