prepare()未定义

时间:2012-07-20 03:14:27

标签: php oop pdo

在过去的两天里,我一直在研究一个项目,无论我尝试什么,我都会遇到某种错误,或者某些东西不起作用。在这一点上,我被告知我正在调用一个未定义的方法,上传:prepare(),这是真的,因为该类被称为上传,语句是$ this-> prepare()....等等。

我很沮丧,无论我做什么,我都无法理解这一点。

classes.php

<?php
require('config.php');
error_reporting(E_ALL);
/*
 * Classes required by the script
 * 
 */

class database extends PDO
{
    public $conURL;

    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();
        }
    }       
}

class upload
{
    public $_FILES;

    public function uploadFile() {
        if ($_FILES['file']['size'] >= 2000000) {
           echo "File is too large!";
        }
        elseif (isset($_FILES['file'])) {
           $stmt = $this->prepare("INSERT INTO upload (name, type, size, content) VALUES (?, ?, ?, ?)");
           $stmt->execute(array($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $_FILES['file']['file']));
        }
    }
}

的config.php

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

upload.php的

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

$dbh = new database($config);
$upload = new upload();
$upload->uploadFile();

2 个答案:

答案 0 :(得分:0)

您试图调用prepare的{​​{1}}函数,而不是upload。试试这个:

database

答案 1 :(得分:0)

我认为您打算使用database->prepare代替this->prepare

您需要将上传类作为数据库实例(或者即时创建)。