简体   繁体   English

我在这个php PDO上做错了什么

[英]What am I doing wrong on this php PDO

So when I run this below it outputs an error Catchable fatal error: Object of class eg could not be converted to string 因此,当我在下面运行此命令时,它将输出错误可捕获的致命错误:类的对象,例如,无法转换为字符串

spl_autoload_register(function($class) {
    require_once '../dbfolder/'.$class.'.php';  
});

$mysql = dbWrapper::getDbInstance();

class eg 
{
    private $username = 'paul';
    private $email = 'paul@yahoo.com';

    public function eg_()
    {
        global $mysql;  

        $sql = "INSERT INTO users(username,email)VALUES(:username,:email)";
        $prepared = $mysql->$this->handler->prepare($sql);

        $prepared->bindParam(':username',$this->username);
        $prepared->bindParam(':email',$this->email);
        $prepared->execute();
    }
}

$eg = new eg();
$eg->eg_();

Please can anyone just point out for me what am doing wrong? 请有人能为我指出做错了什么吗?

This line is the problem: 这行是问题所在:

$prepared = $mysql->$this->handler->prepare($sql);

PHP thinks you're trying to use it's variable variable feature. PHP认为您正在尝试使用它的可变变量功能。 It sees $mysql->$this and tries to convert $this into a string, which the current class does not support (no __toString() magic method). 它看到$mysql->$this并尝试将$this转换$this当前类不支持的字符串(无__toString()魔术方法)。 I assume this is a typo/copy-paste error. 我认为这是一个错字/复制粘贴错误。

To fix it, all you need is to remove it (assuming your $mysql object has a property of handler that is): 要修复它,您只需要删除它(假设您的$mysql对象具有handler属性,即handler

$prepared = $mysql->handler->prepare($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM