当前位置:Gxlcms > PHP教程 > $this->pdo->exec($sql);这是什么意思

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

时间:2021-07-01 10:21:17 帮助过:1人阅读

  1. <code>class test{
  2. protected $pdo;
  3. function delete($sql){
  4. $this->exec($sql);
  5. }
  6. function exec($sql){
  7. echo $this->pdo->exec($sql);
  8. }
  9. }
  10. $a=new test;
  11. $a->delete('ver');</code>

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

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

回复内容:

  1. <code>class test{
  2. protected $pdo;
  3. function delete($sql){
  4. $this->exec($sql);
  5. }
  6. function exec($sql){
  7. echo $this->pdo->exec($sql);
  8. }
  9. }
  10. $a=new test;
  11. $a->delete('ver');</code>

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

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

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

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

因为你没有初始化pdo

  1. <code class="php">public function __construct($pdo)
  2. {
  3. $this->pdo = $pdo;
  4. }</code>

人气教程排行