【问题标题】:PDO Warning and Undefined Method myclassname::bindParams()PDO 警告和未定义方法 myclassname::bindParams()
【发布时间】:2012-07-19 03:59:18
【问题描述】:

我重新开始了我之前正在做的一个项目。在我的 PHP 书籍和 Stack Overflow 上的一些人的帮助下,我能够摆脱我遇到的许多错误。

现在,我得到一个警告和一个致命错误。我理解这两个错误,我只是不确定如何解决我遇到的错误。有人可以帮忙吗?我要求任何人编写代码或只是为我做所有事情,我只需要指出正确的方向。

这是警告,听起来我需要将 __construct 放在 uploadFile() 方法中的某个位置,但同样不确定:

警告:PDO::prepare() [pdo.prepare]: SQLSTATE[00000]: 无错误:PDO 没有在 C:\xampp\htdocs\files\upload.php 中调用构造函数 23

第二个,我完全不知道该怎么办。我意识到错误告诉我什么,我只是不知道从哪里开始修复它。这里是:

致命错误:调用未定义的方法 upload::bindParam() in C:\xampp\htdocs\files\upload.php 第 24 行

如果我可以提供更多信息来让这个问题更容易回答,请告诉我,我会尽力为您获取。感谢您的帮助!

上传.php

  <?php
error_reporting(E_ALL);
require('config.php');

class upload extends PDO
{
    public $conURL, $filename, $tmpname, $filesize, $filetype, $file;

    public function __construct($config) {
        $conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
        parent::__construct($conURL, $config['user'], $config['pass']);

    }       

    public function uploadFile() {
        if ($_FILES['file']['size'] >= 2000000) {
           echo "File is too large!"; 
        }
        else {
            $sth = $this->prepare("INSERT INTO uploads (name, type, size, file) VALUES (?, ?, ?, ?)");
            $sth->bindParam(1, $filename);
            $sth->bindParam(2, $filetype);
            $sth->bindParam(3, $filesize);
            $sth->bindParam(4, $file);
            $sth->execute();
        }
    }
}

$upload = new upload($config);

$upload->filename = $_FILES['file']['name'];
$upload->tmpname = $_FILES['file']['tmp_name'];
$upload->filesize = $_FILES['file']['size'];
$upload->filetype = $_FILES['file']['type'];    
$upload->file = $_FILES['file'];


$upload->uploadFile();

config.php

<?php
$config = array(
        'host' => 'localhost', // db host
        'user' => 'root', // db user
        'pass' => '', //db pass
        'db' => 'files' // db name
);

编辑:感谢这里的所有人,只剩下一个错误,如下:

致命错误:调用未定义的方法 stdClass::uploadFile() in C:\xampp\htdocs\files\upload.php 第 43 行

编辑 2:所有错误都已解决,但没有任何内容插入到数据库中。

【问题讨论】:

  • 您应该在PDOStatement 对象上使用execute()bindParam(),而不是实际的PDO 对象,prepare() 返回这样的对象。
  • 除了@KristerAndersson - class upload extends PDO - 是一个很好的例子,你总是应该更喜欢组合而不是继承

标签: php oop pdo


【解决方案1】:

两个人指出了另一个问题,但这里真正的重点是您正在调用 PDO 子类的 bindParam。那需要pdostatement instaed。试试:

        $sth = $this->prepare("INSERT INTO uploads (name, type, size, file) VALUES (?, ?, ?, ?)");
        $sth->bindParam(1, $this->filename);
        $sth->bindParam(2, $this->filetype);
        $sth->bindParam(3, $this->filesize);
        $sth->bindParam(4, $this->file);
        $sth->execute();

当然,您还需要修复构造函数中的以下错误。

编辑:

我猜构造函数失败并抛出异常。因为 $this->conURL 为空。改变

$conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];

$this->conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];

我之前的回答也改了,请看bindparam()的第二个参数。

【讨论】:

  • 另外,OP 使用未定义的变量,而不是对象属性。我想知道是否有一行没有错误))
【解决方案2】:

您的构造函数应如下所示:

public function __construct($config) {
        $conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
        try {
            parent::__construct($conURL, $config['user'], $config['pass']);
        } catch (PDOException $e) {
            $e->getMessage();
        }
}  

甚至

public function __construct($config)
{
    $conURL = "mysql:host=" . $config['host'] . ";dbname=" . $config['db'];
    parent::__construct($conURL, $config['user'], $config['pass']);
}  

【讨论】:

  • 谢谢!虽然,当我用“parent::__construct”替换“return new PDO”时,它似乎破坏了我的 uploadFile() 方法。现在说该方法在第 43 行(最后一行)上未定义。我在这里错过了什么?
  • @Dustin L.:见 cmets 和另一个答案。您正在调用 PDO 没有的方法
  • 这是我现在得到的错误:致命错误:在第 43 行调用 C:\xampp\htdocs\files\upload.php 中未定义的方法 stdClass::uploadFile()
  • @Dustin L.:将该文件的 40-45 行放到问题中,我们无法猜测(将您的问题附加新文件版本并标记对应行)
【解决方案3】:

因为你在扩展 PDO,所以

return new PDO($this->conURL, $config['user'], $config['pass']);

应该是

parent::__construct($this->conURL, $config['user'], $config['pass']);

【讨论】:

    猜你喜欢
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2012-10-09
    • 1970-01-01
    • 2021-12-02
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多