热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

$this->pdo->exec($sql);这是什么意思

{代码...}看到一个源码是这样写的。(我省略了一部分)为什么是$this->pdo->exec($sql);而不是$this->exec($sql);加上这个PDO有什么作用呢?
class test{
      protected $pdo;
    function delete($sql){
        $this->exec($sql);
    }
    function exec($sql){
        echo $this->pdo->exec($sql);
    }
}
$a=new test;
$a->delete('ver');

看到一个源码是这样写的。(我省略了一部分)

为什么是$this->pdo->exec($sql); 而不是$this->exec($sql);
加上这个PDO有什么作用呢?

回复内容:

class test{
      protected $pdo;
    function delete($sql){
        $this->exec($sql);
    }
    function exec($sql){
        echo $this->pdo->exec($sql);
    }
}
$a=new test;
$a->delete('ver');

看到一个源码是这样写的。(我省略了一部分)

为什么是$this->pdo->exec($sql); 而不是$this->exec($sql);
加上这个PDO有什么作用呢?

protected $pdo;代表的就是和数据的连接。当需要执行SQL命令时,当然需要用到这个变量啊。

估计 __construct 中 有 $this->pdo = new PDO();之类的
$this->pdo 其实是指向了PDO这个类
$this 只是当前类
上面你的报错信息是说 你的 $sql 这个参数没传

因为你没有初始化pdo

public function __construct($pdo)
{
    $this->pdo = $pdo;
}

推荐阅读
author-avatar
白杨树
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有