Heitor Victor
Heitor Victor

Reputation: 11

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\wamp64\www\empresa\model\Model_user.php on line 21

My code is giving this error:

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\wamp64\www\empresa\model\Model_user.php on line 21

My classes are these:

Connection.php

<?php

class Connection 
{
    private $host = "localhost";
    private $user = "root";
    private $pass = "Heitor1231";
    private $bank = "allfiles";
    private $conn ;

    public function __construct()
    {
        try
        {
            $conn = mysqli_connect($this->host,$this->user,$this->pass,$this->bank);
            return $conn;
            echo "Conexão feita com sucesso!";
        }
        catch (Exception $e)
        {
            echo "Erro {$e->getMessage()}";
            return null;
        }
    }

    public function desconnection()
    {
        try {
            mysqli_close($this->$conn);
        } catch (Exception $e) {
            echo "Erro {$e->getMessage()}";
        }
    }

}

?>

Model_user.php

<?php

    /**
    * 
    */
    include_once "Connection.php";
    class Model_user
    {

        function __construct()
        {
            # code...
        }

        public function insert ($firstName,$lastName,$telephone,$email,$nickName,$password)
        {
            try
            {
                $sql = "INSERT INTO `users` (`id_user`, `firstName`, `lastName`, `birth`, `cpf`, `cnpj`, `city`, `state`, `address`, `number`, `telephone`, `email`, `nickname`, `password`) VALUES (NULL, {$firstName}, {$lastName}, NULL, NULL, NULL, NULL, NULL, NULL, NULL, {$telephone}, {$email}, {$nickName}, {$password})";
                $con = new Connection();
                mysqli_query($con,$sql);
                $_SESSION['message'] = 'Registro cadastrado com sucesso.';
                $_SESSION['type'] = 'success';
            }
            catch (Exception $e) { 
                $_SESSION['message'] = 'Nao foi possivel realizar a operacao.';
                $_SESSION['type'] = 'danger';
            } 

            $con->desconnection();

        }
    }
?>

User.php

<?php

    /**
     *
     */
    require_once "CRUDS.php";
    require "../model/Model_user.php";

    class User implements CRUDS
    {

        private $id_user;
        private $firstName;   
        private $lastName;
        private $birth;
        private $cpf;
        private $cnpj;
        private $city;
        private $state;
        private $address;
        private $number;
        private $telephone;
        private $email;
        private $nickName;
        private $password;

        public function __construct()
        {
            $this->setFirstName($_POST['firstName']);   
            $this->setLastName($_POST['lastName']);
            $this->setTelephone($_POST['tel']);
            $this->setEmail($_POST['email']);
            $this->setNickName($_POST['user']);
            $this->setPassword($_POST['password']);
            if(($this->getFirstName() != "") || ($this->getFirstName() != NULL)){
                $this->create();
            }
        }

        public function create()
        {
            $bank = new Model_user();
            $bank->insert($this->getFirstName(),$this->getLastName(),$this->getTelephone(),$this->getEmail(),$this->getNickName(),
                          $this->getPassword());

        }


        public function read()
        {
            // TODO: implement here
        }


        public function update()
        {
            // TODO: implement here
        }


        public function delete()
        {
            // TODO: implement here
        }


        public function select()
        {
            // TODO: implement here
        }


        public function logoff()
        {
            // TODO: implement here
        }


        public function login()
        {
            echo "<script>alerta()</script>";
        }


        public function getId_user()
        {
            return $this->id_user;
        }

        public function setId_user($id_user)
        {
            $this->id_user = $id_user;
        }

        public function getFirstName()
        {
            return $this->firstName;
        }

        public function setFirstName($firstName)
        {
            $this->firstName = $firstName;
        }

        public function getLastName()
        {

            return $this->lastName;
        }

        public function setLastName($lastName)
        {
            $this->lastName = $lastName;
        }

        public function getBirth()
        {
            return $this->birth;
        }

        public function setBirth($birth)
        {
            $this->birth = $birth;
        }

        public function getCpf()
        {

        }

        public function setCpf()
        {
            // TODO: implement here
        }

        public function getCnpj()
        {
            // TODO: implement here
        }

        public function setCnpj()
        {
            // TODO: implement here
        }

        public function getCity()
        {
            // TODO: implement here
        }

        public function setCity()
        {
            // TODO: implement here
        }

        public function getState()
        {
            // TODO: implement here
        }

        public function setState()
        {
            // TODO: implement here
        }

        public function getAddress()
        {
            // TODO: implement here
        }

        public function setAddress()
        {
            // TODO: implement here
        }

        public function getNumber()
        {
            // TODO: implement here
        }

        public function setNumber()
        {
            // TODO: implement here
        }

        public function getTelephone()
        {
            // TODO: implement here
        }

        public function setTelephone()
        {
            // TODO: implement here
        }

        public function getEmail()
        {
            // TODO: implement here
        }

        public function setEmail()
        {
            // TODO: implement here
        }

        public function getNickName()
        {
            // TODO: implement here
        }

        public function setNickName()
        {
            // TODO: implement here
        }

        public function getPassword()
        {
            // TODO: implement here
        }

        public function setPassword()
        {
            // TODO: implement here
        }
    }

    $user = new User();
?>

Upvotes: 0

Views: 932

Answers (1)

Haris
Haris

Reputation: 763

The problem is that you are using class object as connection parameter. I am providing you an alternate solution.Replace your constructor with this function in your Connection.php:

    public function connection()
        {
            try
            {
                $conn = mysqli_connect($this->host,$this->user,$this->pass,$this->bank);
                echo "Conexão feita com sucesso!";                
                return $conn;
            }
            catch (Exception $e)
            {
                echo "Erro {$e->getMessage()}";
                return null;
            }
        }

Now in Model_user.php do this use object to call connection function. Place these lines after you create the object.

$conn=$con->connection();

And then use the new parameter.

 mysqli_query($conn,$sql);

And this will work fine.

Upvotes: 0

Related Questions