Prepared inquiry and Array

0

I've done a CRUD of people with queries prepared in PDO that worked and now they have sent me to do it in a way that I do not understand, which is passing the values to use in array in execute() . For example, to create a person I put this and it worked for me:

$stmt=conectar()->prepare("INSERT INTO persona (codigo,nombre) VALUES (:codigo,:nombre)");
$stmt->bindParam(':codigo',$codigo);
$stmt->bindParam(':nombre',$nombre);
$codigo=$_GET['codigo'];
$nombre=$_GET['nombre'];
$stmt->execute();
$texto="Persona creada!";

Now I have searched the internet and I have written this:

$stmt=conectar()->prepare("INSERT INTO persona (codigo,nombre) VALUES (:codigo,:nombre)");
$stmt->execute(array(':codigo'->$codigo,':nombre'=>$nombre));

And he says:

  

Notice: Trying to get property of non-object in   C: \ xampp \ htdocs \ xampp \ ad \ people \ new.php on line 59   SQLSTATE [HY093]: Invalid parameter number: parameter was not defined

Since it did not work for me, I wrote this:

$stmt=conectar()->prepare("INSERT INTO persona (codigo,nombre) VALUES (:codigo,:nombre)");
$persona=new Persona($codigo,$nombre);
$stmt->execute((array)$persona);

And I get the following error:

  

SQLSTATE [23000]: Integrity constraint violation: 1048 Column 'code'   can not be null   My Person class is this:

class Persona{
        private $codigo;
        private $nombre;
        function __construct($codigo,$nombre){
            $this->codigo=$codigo;
            $this->nombre=$nombre;
        }
        function getCodigo(){
            return $this->$codigo;
        }
        function setCodigo($codigo){
            $this->codigo=$codigo;
        }
        function getNombre(){
            return $this->$nombre;
        }
        function setNombre($nombre){
            $this->nombre=$nombre;
        }
    }
    
asked by Charly Utrilla 13.01.2018 в 19:25
source

1 answer

1

The second option the array is poorly constructed, the - > is used to refer to methods or attributes of a class, the correct thing is = > for which you are passing incorrect number of parameters

$stmt->execute(array(':codigo'->$codigo,':nombre'=>$nombre));
// por
$stmt->execute(array(':codigo'=>$codigo,':nombre'=>$nombre));

For the last case, according to your comments the error is in the name of the constructor, the correct thing is __construct , besides placing your fields as private.

private $codigo; 
private $nombre; 

function __construct($codigo,$nombre){ 
    $this->codigo= $codigo; 
    $this->nombre= $nombre; 
} 

Edit

How is it advisable to use the private attributes of your class, when converting% of that object into% you will have as keys the name of the attributes in addition to the name of the class, therefore no parameter would be happening to your execution of the query (ejm)

( [codigo:Persona:private] => 12365 [nombre:Persona:private] => Stack) 

The solution would be to modify% attribute% of each attribute but it would not be correct. maybe I can create a method within the person class that returns said array to be inserted or used from another part of its code

Persona.php

function getArrayData(){
        return array('nombre'=> $this->nombre , 'codigo' => $this->codigo);
}

To then call

$stmt->execute($persona->getArrayData());
    
answered by 13.01.2018 / 19:48
source