2

How can I implement mysqli in an extended class?

I am uploading an image and storing it in a MySQL database, but I get this error:

Notice: Undefined variable: mysqli in ...ecc/ecc/ on line 33

Fatal error: Call to a member function query() on a non-object in ...ecc/ecc/ on line 33

Here is my test code:

<?php
    interface ICheckImage {
        public function checkImage();
        public function sendImage();
    }           
    abstract class ACheckImage implements ICheckImage {
        public $image;
        private $mysqli;    
        public function _construct(){
            $this->image = $_POST['image'];
            $this->mysqli = new mysqli('localhost','test','test','test');
        }
    }           
    class Check extends ACheckImage {
        public function checkImage() {
            if($this->image > 102400) {
                echo "File troppo grande";
            } 
        }
        public function sendImage() {
            //This is the line 33 give me the error
            if ($mysqli->query("INSERT INTO images (image) VALUES ('$this->image')")) {
            echo "Upload avvenuto &nbsp";
            } else {
                echo "Errore &nbsp" . $mysqli->error;
            }  
        }
    }   
    $form = new Check();
    $form->checkImage();    
    $form->sendImage();
?>

1 Answer 1

3

There are some errors in your code.

  1. The $mysqli member is private inside the abstract class. It will not be inherited by the Check class, so it does not exist there. Make it protected.

  2. Access to the members of a class always needs $this-> in front, specifically $this->mysqli in this instance.

  3. The constructor function must be named __construct with two underscores in front.

  4. The image check looks wrong. $_POST['image'] does contain something that you expect to store in the database, but you also compare it with an integer value and seem to echo an error message if it is bigger. While the data handling will work, e.g. you can compare a string from POST data with an integer, it looks like you want something else.

Sign up to request clarification or add additional context in comments.

7 Comments

...and the OP also needs to refer to $this->mysqli->query(), not $mysqli directly (a local variable).
thanks, the first error passed :) but give me the same error now Fatal error: Call to a member function query() on a non-object in ..ecc/..ecc/ on line 33
@user2041211: You don't appear to be calling the parent constructor (that establishes the db connection) from the extended class? (That error suggests you don't have a database connection.)
If the extending class does not have a constructor on it's own, the parent class' constructor will be used if present.
Shouldn't the constructor begin with two underscores __ instead of one?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.