PDO错误-在非对象上调用成员函数prepare()

汤姆·伯德

我创建的上载文件的类遇到问题。此类有一个父类,它是数据库访问层。上传看起来像这样:

<?php

class upload extends bao {

    // general variables
    private $basePath = '../../uploads/';
    private $fullMaxWidth = 1000;
    private $mediumMaxWidth = 500;
    private $smallMaxWidth = 250;
    private $errors = 0;
    private $difference = 0;
    private $debugging = false;

    private $maxFileSize = 100000000;

    // current file variables
    private $currFileName = '';
    private $currFileType = '';
    private $currFileTmpName = '';
    private $currFileError = 0;
    private $currFileSize = 0;
    private $currExt = '';

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        This will overwrite any default values with whats supplied

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
    public function __construct( $params = null ) {

        if($this -> debugging) {
            echo '__construct method';
        }

        if( $params ) {
            foreach( $params as $key => $val ) {
                $this -> $key = $val;
            }
        }

        $this -> initiate();
    }

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Initiates the upload process

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
    private function initiate() {

        if( $this -> debugging ) {
            echo 'initiate method';
        }

        for($i = 0; $i < count($_FILES['files']['name']); $i++) {
            $fileCheck = $this -> checkFileSize( $_FILES['files']['size'][$i] );
            if(!$fileCheck) {
                $this -> errors++;
            }

            $this -> currFileType = $_FILES['files']['type'][$i];
            $this -> currFileTmpName = $_FILES['files']['tmp_name'][$i];
            $fileExtPath = pathinfo($_FILES['files']['name'][$i]);
            $this -> currExt = strtolower($fileExtPath['extension']);

            if($this -> errors < 1) {
                $this -> switcher();
            }

        }

    }

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Runs a check to see what the file size is

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
    private function checkFileSize( $fileSize ) {

        if($this -> debugging) {
            echo 'checkFileSize method';
        }

        if($fileSize <= $this -> maxFileSize) {
            return true;
        } else {
            return false;
        }

    }

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Determines which type of file it is

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
    private function switcher() {

        if($this -> debugging) {
            echo 'switcher method';
        }

        if( $this -> currFileType == 'image/gif' || $this -> currFileType == 'image/png' || $this -> currFileType == 'image/jpeg') {
            $this -> image();
        } else {
            $this -> other();
        }
    }

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Uploads all image files

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
    private function image() {

        if($this -> debugging) {
            echo 'image method';
        }

        $rand = rand(1000, 10000);
        $newFullName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-full.'. $this -> currExt;
        $newMediumName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-medium.'. $this -> currExt;
        $newSmallName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-small.'. $this -> currExt;
        $fullPath = $this -> basePath . $newFileName;   

        $info = getimagesize( $this -> currFileTmpName );

        $fullWidth = $this -> getNewWidth( $this -> fullMaxWidth, $info[0] );
        $mediumWidth = $this -> getNewWidth( $this -> mediumMaxWidth, $info[0] );
        $smallWidth = $this -> getNewWidth( $this -> smallMaxWidth, $info[0] );

        $fullDif = $this -> setDifference( $this -> fullMaxWidth, $info[0] );
        $mediumDif = $this -> setDifference( $this -> mediumMaxWidth, $info[0] );
        $smallDif = $this -> setDifference( $this -> smallMaxWidth, $info[0] );

        $fullHeight = $this -> getNewHeight( $info[1], $fullDif );
        $mediumHeight = $this -> getNewHeight( $info[1], $mediumDif );
        $smallHeight = $this -> getNewHeight( $info[1], $smallDif );


        // set debugging to try to output new image sizes
        if( $this -> debugging ) {
            echo $fullWidth .' - '. $fullHeight .'<br/>';
            echo $mediumWidth .' - '. $mediumHeight .'<br/>';
            echo $smallWidth .' - '. $smallHeight .'<br/>';
        }

        $smallImage = imagecreatetruecolor($smallWidth, $smallHeight);
        $mediumImage = imagecreatetruecolor($mediumWidth, $mediumHeight);
        $fullImage = imagecreatetruecolor($fullWidth, $fullHeight);


        switch($info['mime']) {
            case 'image/gif':
                $smallImg = imagecreatefromgif( $this -> currFileTmpName );
                $mediumImg = imagecreatefromgif( $this -> currFileTmpName );
                $fullImg = imagecreatefromgif( $this -> currFileTmpName );
            break;

            case 'image/png':
                $smallImg = imagecreatefrompng( $this -> currFileTmpName );
                $mediumImg = imagecreatefrompng( $this -> currFileTmpName );
                $fullImg = imagecreatefrompng( $this -> currFileTmpName );
            break;

            default:
                $smallImg = imagecreatefromjpeg( $this -> currFileTmpName );
                $mediumImg = imagecreatefromjpeg( $this -> currFileTmpName );
                $fullImg = imagecreatefromjpeg( $this -> currFileTmpName );
            break;
        }

        imagecopyresampled($smallImage, $smallImg, 0, 0, 0, 0, $smallWidth, $smallHeight, $info[0], $info[1] );
        imagecopyresampled($mediumImage, $mediumImg, 0, 0, 0, 0, $mediumWidth, $mediumHeight, $info[0], $info[1] );
        imagecopyresampled($fullImage, $fullImg, 0, 0, 0, 0, $fullWidth, $fullHeight, $info[0], $info[1] );


         if($info['mime'] == 'image/gif') {
              imagegif( $smallImage, $this -> basePath . $newSmallName );
              imagegif( $mediumImage, $this -> basePath . $newMediumName );
              imagegif( $fullImage, $this -> basePath . $newFullName );
         } elseif($info['mime'] == 'image/png') {
              imagepng( $smallImage, $this -> basePath . $newSmallName, 8);
              imagepng( $mediumImage, $this -> basePath . $newMediumName, 8);
              imagepng( $fullImage, $this -> basePath . $newFullName, 8);
         } else {
              imagejpeg( $smallImage, $this -> basePath . $newSmallName, 80);
              imagejpeg( $mediumImage, $this -> basePath . $newMediumName, 80);
              imagejpeg( $fullImage, $this -> basePath . $newFullName, 80);
         }

        // insert into database
        $stringQuery = "INSERT INTO uploads (uid,smallFilename,mediumFilename,fullFilename,typeFile,status,createdOn,modifiedOn)VALUES(:uid, :smallFilename, :mediumFilename, :fullFilename, :typeFile, :status, :createdOn, :modifiedOn )";
        $binding = array(":uid" => $_COOKIE['userId'], ":smallFilename" => $newSmallName, ":mediumFilename" => $newMediumName, ":fullFilename" => $newFullName, ":typeFile" => $this -> currExt, ":status" => "new", ":createdOn" => currentTime, ":modifiedOn" => currentTime );

        $insertDB = parent::query( $stringQuery, $binding );

         if( !$insertDB ) {
            $this -> errors++;
         }
    }

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Uploads all files minus images

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
    private function other() {

        if($this -> debugging) {
            echo 'other method';
        }

        $rand = rand(1000, 10000);
        $newFullName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'.'. $this -> currExt;
        $fullPath = $this -> basePath . $newFullName;

        if(move_uploaded_file($this -> currFileTmpName, $fullPath)){

            $stringQuery = "INSERT INTO uploads (uid,fullFilename,title,description,keywords,typeFile,uploadedType,uploadedId,status,createdOn,modifiedOn)VALUES(:uid,:fullFilename,:typeFile,:uploadedType,:uploadedId,:status,:createdOn,:modifiedOn)";
            $binding = array( ":uid" => $_COOKIE['userId'], ":fullFilename" => $newFullName, ":typeFile" => $this -> currExt, ":uploadedType" => "", ":uploadedId" => "", ":status" => "new", ":createdOn" => currentTime, ":modifiedOn" => currentTime );

            $insertDB = parent::query( $stringQuery, $binding );

            if( !$insertDB ) {
                $this -> errors++;
            }

        } else {
            echo 'There was an error';
        }
    }

    /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

        Utility methods

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

    // if image does not match size set the difference
    private function setDifference( $fullwidth, $width ) {

        if($this -> debugging) {
            echo 'setDifference method';
        }

        return $fullwidth / $width;
    }

    // calculate the new width
    private function getNewWidth( $fullwidth, $width ) {

        if($this -> debugging) {
            echo 'getNewWidth method';
        }

        $this -> difference = $fullwidth / $width;
        return round($width * $this -> difference);
    }

    // calculate the new height
    private function getNewHeight( $height, $diff ) {

        if($this -> debugging) {
            echo 'getNewHeight method';
        }

        $newHeight = round($height * $diff );
        return $newHeight;
    }

    // creates new image for 3 sizes
    private function createImageTrueFull( $width, $height ) {

        if($this -> debugging) {
            echo 'createImageTrueFull method';
        }

        return imagecreatetruecolor( $width, $height );
    }
}

?>

基本数据库访问层如下所示:

<?php

class bao {

    private $environments = array(

        'development' => array(

        ),

        'test' => array(

        ), 

        'review' => array(

        ), 

        'production' => array(

        ), 

    );

    private     $url        = '',
                $host       = '',
                $dbname     = '',
                $user       = '',
                $pass       = '';

    /*
        Variable for connection
    */
    private     $connection;

    /*
        Init method
    */
    public function __construct( $params = null ) {

        $this -> setConnectionData();
        $this -> overwriteSettings( $params );
        $connection = $this -> connect();

    }

    public function setConnectionData() {

        if( strpos( $_SERVER['HTTP_HOST'], 'dev' ) ) {
            $envVariable = 'development';
        } elseif( strpos( $_SERVER['HTTP_HOST'], 'test' ) ) {
            $envVariable = 'test';
        } elseif( strpos( $_SERVER['HTTP_HOST'], 'review' ) ) {
            $envVariable = 'review';
        } else {
            $envVariable = 'production';
        }

        $this -> url = $this -> environments[$envVariable]['url'];
        $this -> host = $this -> environments[$envVariable]['host'];
        $this -> dbname = $this -> environments[$envVariable]['dbname'];
        $this -> user = $this -> environments[$envVariable]['user'];
        $this -> pass = $this -> environments[$envVariable]['pass'];
    }

    /*
        Overwrite any required values from default
    */
    private function overwriteSettings( $params ) {
        if( $params ) {
            foreach( $params as $key => $val ) {
                $this -> $key = $val;
            }
        }
    }

    /*
        Connect to database and then assign to variable
    */
    private function connect() {
        $this -> connection = new PDO( sprintf( 'mysql:host=%s;dbname=%s', $this -> host, $this -> dbname ), $this -> user, $this -> pass );
    }

    public function openQuery( $query, $params = null ) {

        if( $params ) {
            return $this -> query( $query, $params );
        } else {
            return $this -> query( $query );
        }
    }

    public function openFetchall( $sth ) {
        return $this -> fetchAll( $sth );
    }

    /*
        Runs actual query
    */
    protected function query( $query, $params = null ) {

        $sth = $this -> connection -> prepare( $query );

        if( $params ) {
            $sth -> execute( $params );
        } else {
            $sth -> execute();
        }

        return $sth;
    }

    public function openNumRows( $sth ) {
        return $this -> numRows( $sth );
    }

    protected function numRows( $sth ) {
        return $sth -> rowCount();
    }

    public function openFetch( $sth ) {
        return $this -> fetch( $sth );
    }

    protected function fetch( $sth ) {
        return $sth -> fetch( PDO::FETCH_ASSOC );
    }

    protected function fetchAll( $sth ) {
        return $sth -> fetchAll( PDO::FETCH_ASSOC );
    }

    protected function lastId( $sth ) {
        return $this -> connection -> lastInsertId();
    }

}

?>

现在,当我运行脚本时,出现以下错误:

Fatal error: Call to a member function prepare() on a non-object in /home/content/51/8932751/html/sites/clients/OwnerBusinessLoans/www/dev/lib/helpers/bao.php on line 118

我不明白为什么这样,因为我只是upload以与我所有其他查询相同的方式类中运行准备好的查询,并且它们运行得很好,并且没有显示此错误。关于语法和我做事的方式,这是我在十多个其他类中所做的方式,而且我从未收到任何错误。

在我的bao课堂上,我确实删除了连接详细信息,但是它们在我测试应用程序的地方。

起初我以为是查询语法或其他什么东西,所以我更改并检查并重新检查了我所有的语法,这看起来还不错。如果是我的PDO实例未定义,我不明白为什么,因为它在所有其他类中都以相同的方式定义并使用相同的方式。

任何帮助将不胜感激,我一直在为此努力,我觉得它必须很小!

谢谢

菲尔

您的扩展类需要调用父构造函数,以执行bao包括创建数据库连接在内构造序列。例如...

class upload extends bao {
    // snip

    public function __construct($params = null) {
        parent::__construct($params);

        // now the rest of the upload class code

具体参见http://php.net/manual/language.oop5.decon.php

注意:如果子类定义了构造函数,则不会隐式调用父构造函数。为了运行父构造函数,需要在子构造函数内调用parent :: __ construct()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PDO致命错误-在非对象上调用成员函数prepare()

来自分类Dev

PDO致命错误-在非对象上调用成员函数prepare()

来自分类Dev

在PDO的非对象上调用成员函数prepare()

来自分类Dev

致命错误:在非对象错误上调用成员函数prepare()

来自分类Dev

致命错误:在非对象错误上调用成员函数prepare()

来自分类Dev

在非对象上调用成员函数prepare()错误消息

来自分类Dev

PHP致命错误:在非对象上调用成员函数prepare()

来自分类Dev

“致命错误:在非对象上调用成员函数prepare()。”

来自分类Dev

致命错误:“在非对象上调用成员函数prepare()”

来自分类Dev

PDO-致命错误:在非对象上调用成员函数prepare()

来自分类Dev

PDO致命错误:在非对象上调用成员函数prepare()(使用类)

来自分类Dev

致命错误:在非对象上调用成员函数prepare(),调用prepare语句的对象不为null

来自分类Dev

PHP pdo类getadresses警告并在非对象上调用成员函数prepare()

来自分类Dev

使用PHP在PDO中的非对象上调用成员函数prepare()

来自分类Dev

致命错误:在第16行上的非对象上调用成员函数prepare()

来自分类Dev

致命错误:在第15行的非对象上调用成员函数prepare()

来自分类Dev

调用非对象 PHP PDO 上的成员函数 prepare()

来自分类Dev

PDO-致命错误:在非对象上调用成员函数fetch()

来自分类Dev

PDO页面中的错误在非对象上调用成员函数bindValue()

来自分类Dev

PDO页面中的错误在非对象上调用成员函数bindValue()

来自分类Dev

错误致命错误:在非对象上调用成员函数insert()

来自分类Dev

致命错误:在非对象上调用成员函数insert()

来自分类Dev

致命错误:在非对象上调用成员函数error()

来自分类Dev

致命错误:在非对象上调用成员函数first()

来自分类Dev

Symfony 2错误:在非对象上调用成员函数get()

来自分类Dev

错误:在非对象上调用成员函数paginate()

来自分类Dev

FatalErrorException:错误:在非对象symfony上调用成员函数has()

来自分类Dev

错误:在非对象上调用成员函数get()

来自分类Dev

FatalErrorException:错误:在非对象上调用成员函数has()

Related 相关文章

  1. 1

    PDO致命错误-在非对象上调用成员函数prepare()

  2. 2

    PDO致命错误-在非对象上调用成员函数prepare()

  3. 3

    在PDO的非对象上调用成员函数prepare()

  4. 4

    致命错误:在非对象错误上调用成员函数prepare()

  5. 5

    致命错误:在非对象错误上调用成员函数prepare()

  6. 6

    在非对象上调用成员函数prepare()错误消息

  7. 7

    PHP致命错误:在非对象上调用成员函数prepare()

  8. 8

    “致命错误:在非对象上调用成员函数prepare()。”

  9. 9

    致命错误:“在非对象上调用成员函数prepare()”

  10. 10

    PDO-致命错误:在非对象上调用成员函数prepare()

  11. 11

    PDO致命错误:在非对象上调用成员函数prepare()(使用类)

  12. 12

    致命错误:在非对象上调用成员函数prepare(),调用prepare语句的对象不为null

  13. 13

    PHP pdo类getadresses警告并在非对象上调用成员函数prepare()

  14. 14

    使用PHP在PDO中的非对象上调用成员函数prepare()

  15. 15

    致命错误:在第16行上的非对象上调用成员函数prepare()

  16. 16

    致命错误:在第15行的非对象上调用成员函数prepare()

  17. 17

    调用非对象 PHP PDO 上的成员函数 prepare()

  18. 18

    PDO-致命错误:在非对象上调用成员函数fetch()

  19. 19

    PDO页面中的错误在非对象上调用成员函数bindValue()

  20. 20

    PDO页面中的错误在非对象上调用成员函数bindValue()

  21. 21

    错误致命错误:在非对象上调用成员函数insert()

  22. 22

    致命错误:在非对象上调用成员函数insert()

  23. 23

    致命错误:在非对象上调用成员函数error()

  24. 24

    致命错误:在非对象上调用成员函数first()

  25. 25

    Symfony 2错误:在非对象上调用成员函数get()

  26. 26

    错误:在非对象上调用成员函数paginate()

  27. 27

    FatalErrorException:错误:在非对象symfony上调用成员函数has()

  28. 28

    错误:在非对象上调用成员函数get()

  29. 29

    FatalErrorException:错误:在非对象上调用成员函数has()

热门标签

归档