Fatal error: Uncaught ArgumentCountError

0

Can you give me a hand with this error? I'm new to this ..

  

Fatal error: Uncaught ArgumentCountError: Too few arguments to   function Base :: bind (), 2 passed in   C: \ xampp \ htdocs \ guiacordoba \ app \ models \ User.php online 21 and   exactly 3 expected in   C: \ xampp \ htdocs \ guiacordoba \ app \ libraries \ Base.php: 39 Stack trace: # 0   C: \ xampp \ htdocs \ guiacordoba \ app \ models \ User.php (21):   Base-> bind (': name', 'jhghj') # 1   C: \ xampp \ htdocs \ guiacordoba \ app \ drivers \ Pages.php (27):   User-> addUser (Array) # 2   C: \ xampp \ htdocs \ guiacordoba \ app \ libraries \ Core.php (51):   Pages-> add () # 3 C: \ xampp \ htdocs \ guiacordoba \ public \ index.php (6):   Core-> __ construct () # 4 {main} thrown in   C: \ xampp \ htdocs \ guiacordoba \ app \ libraries \ Base.php on line 39

Base.php

<?php

  //Clase para conectarse a la base de datos y ejecutar consultas PDO
  class Base{
    private $host = DB_HOST;
    private $usuario = DB_USUARIO;
    private $password = DB_PASSWORD;
    private $db_nombre = DB_NOMBRE;

    private $dbh;
    private $stmt;
    private $error;

    public function __construct(){
      //configurar conexion
      $dsn = 'mysql:host=' . $this->host . ';dbname=' .$this->db_nombre;

      $opciones = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
      );

      //Crear una instancia de PDO
      try {
          $this->dbh = new PDO($dsn, $this->usuario, $this->password, $opciones);
          $this->dbh->exec('set names utf8');

      } catch (PDOException $e) {
          $this->error = $e->getMessage();
          echo $this->error;
      }
    }

    //Preparamos la consulta
    public function query($sql){
      $this->stmt = $this->dbh->prepare($sql);
    }

    //Vinculamos la consulta con bind
    public function bind($parametro, $valor, $tipo){
      if (is_null($tipo)) {
        switch (true) {
          case is_int($valor):
            $tipo = PDO::PARAM_INT;
          break;
          case is_bool($valor):
            $tipo = PDO::PARAM_BOOL;
          break;
          case is_null($valor):
            $tipo = PDO::PARAM_NULL;
          break;
          default:
            $tipo = PDO::PARAM_STR;
          break;
        }
      }
      $this->stmt->bindValue($parametro, $valor, $tipo);
    }

    //Ejecuta la consulta
    public function execute(){
      return $this->stmt->execute();
    }

    //Obtener los registros de la consulta
    public function registros(){
      $this->execute();
      return $this->stmt->fetchAll(PDO::FETCH_OBJ);
    }

    //Obtener un unico registro
    public function registro(){
      $this->execute();
      return $this->stmt->fetch(PDO::FETCH_OBJ);
    }

    //Obtener cantidad de filas con el metodo rowCount
    public function rowCount(){
      return $this->stmt->rowCount();
    }
  }

User.php

<?php

  class Usuario{
    private $db;

    public function __construct(){
      $this->db = new Base;
    }

    public function obtenerUsuarios(){
      $this->db->query('SELECT * FROM clientes');

      $resultados = $this->db->registros();

      return $resultados;
    }

    public function agregarUsuario($datos){
      $this->db->query('INSERT INTO clientes (nombre, email, telefono) VALUES (:nombre, :email, :telefono)');

      $this->db->bind(':nombre', $datos['nombre']);
      $this->db->bind(':email', $datos['email']);
      $this->db->bind(':telefono', $datos['telefono']);

      if ($this->db->execute()) {
        return true;
      }else{
        return false;
      }
    }
  }

I hope you can help me, thank you very much!

    
asked by Max Menvielle 08.08.2018 в 12:09
source

2 answers

0

The problem is that your function bind() expects 3 parameters: ($parametro, $valor, $tipo) :

But you only spend 2 on the following functions:

$this->db->bind(':nombre', $datos['nombre']);
$this->db->bind(':email', $datos['email']);
$this->db->bind(':telefono', $datos['telefono']);

And so that you do not get an error, you have to:

  • Pass 3 parameters
  • Or else prepare the function to work without the need to enter the 3 parameters
answered by 08.08.2018 / 13:33
source
0

The problem is that PHP expects that the arguments of the function exist. If you do not give it to you, you do not initialize it, they do not exist.

here you are sending 2

$this->db->bind(':nombre', $datos['nombre']); 

here wait 3

public function bind($parametro, $valor, $tipo)

What I propose is that you initiate the argument so that in case you do not pass it, at least it exists.

public function bind($parametro, $valor, $tipo = NULL) {

You have more information about arguments here

    
answered by 08.08.2018 в 13:32