Foros del Web » Programando para Internet » PHP » Frameworks y PHP orientado a objetos »

PHP OO problema con pdo e insert y parametro

Estas en el tema de problema con pdo e insert y parametro en el foro de Frameworks y PHP orientado a objetos en Foros del Web. hola tengo lo siguiente es un insert simple a una tabla . Código PHP: $daily  =  '{"amount":0,"accumulated":0}' ; $this -> money  =  10 ;                              ...
  #1 (permalink)  
Antiguo 21/01/2012, 11:36
Avatar de RIVERMILLOS  
Fecha de Ingreso: marzo-2010
Mensajes: 1.336
Antigüedad: 14 años, 1 mes
Puntos: 15
problema con pdo e insert y parametro

hola tengo lo siguiente es un insert simple a una tabla .

Código PHP:

$daily 
'{"amount":0,"accumulated":0}';
$this->money 10;
                            
$insert 'INSERT INTO usuarios (idufb,nick,money,gas,phone,address,mail,daily
,ultiposi,idlevel,idavatars,city,idciud
ad, dailyrents, dailytimeto)
 VALUES (? , ? , ? , ? , ? , ? ,?, NOW(), ? , 1, 1, ? ,?,'
.$daily.','.$daily.')';

                            
$stmt_insert $this->conn->prepare$insert ); 

                            
$stmt_insert->bindParam($this->idufb);
                            
$stmt_insert->bindParam($this->nickname);
                            
$stmt_insert->bindParam($this->money);
                            
$stmt_insert->bindParam($this->gas);
                            
$stmt_insert->bindParam($this->phone);
                            
$stmt_insert->bindParam($this->address);
                            
$stmt_insert->bindParam($this->mail);
                            
$stmt_insert->bindParam($this->point);
                            
$stmt_insert->bindParam(9,  $city);
                            
$stmt_insert->bindParam(10$idcity); 
y me sale este error

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

no se que podria ser..


saludos
  #2 (permalink)  
Antiguo 23/01/2012, 09:01
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: problema con pdo e insert y parametro

Antes de hacer el prepare, realiza un echo de tu query, parece que esta mal formada.

Saludos.
  #3 (permalink)  
Antiguo 23/01/2012, 09:09
Avatar de RIVERMILLOS  
Fecha de Ingreso: marzo-2010
Mensajes: 1.336
Antigüedad: 14 años, 1 mes
Puntos: 15
Respuesta: problema con pdo e insert y parametro

Hola muchas gracias por tu respuesta haciendo pruebas , cambie la consulta con asii y tampoco me funciona con bindparam y lo cambie a bindValue y si.

Código PHP:
Ver original
  1. $daily = '{"amount":0,"accumulated":0}';
  2.                            
  3.                             $insert = 'INSERT INTO usuarios (idufb,nick,money,gas,phone,address,mail,daily
  4.                            ,ultiposi,idlevel,idavatars,city,idciudad, dailyrents, dailytimeto)
  5.                            VALUES (? , ? , ? , ? , ? , ? ,?, NOW(), ? , 1, 1, ? ,? , ? ,?)';
  6.  
  7.                             $stmt_insert = $this->conn->prepare( $insert );
  8.  
  9.                             $stmt_insert->bindValue(1 , $this->idufb,PDO::PARAM_STR);
  10.                             $stmt_insert->bindValue(2 , $this->nickname,PDO::PARAM_STR);
  11.                             $stmt_insert->bindValue(3 , $this->money,PDO::PARAM_INT);
  12.                             $stmt_insert->bindValue(4 , $this->gas,PDO::PARAM_STR);
  13.                             $stmt_insert->bindValue(5 , $this->phone,PDO::PARAM_STR);
  14.                             $stmt_insert->bindValue(6 , $this->address,PDO::PARAM_STR);
  15.                             $stmt_insert->bindValue(7 , $this->mail,PDO::PARAM_STR);
  16.                             $stmt_insert->bindValue(8 , $this->point,PDO::PARAM_STR);
  17.                             $stmt_insert->bindValue(9,  $city,PDO::PARAM_STR);
  18.                             $stmt_insert->bindValue(10, $idcity,PDO::PARAM_STR);
  19.                             $stmt_insert->bindValue(11, $daily,PDO::PARAM_STR);
  20.                             $stmt_insert->bindValue(12, $daily,PDO::PARAM_STR);

con bindValue me funciona no se porque con bindParam no ?


saludos
  #4 (permalink)  
Antiguo 23/01/2012, 09:57
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: problema con pdo e insert y parametro

Ah, la razón es que bindParam, hace un enlace a la variable (para variables input/output), y bindValue hace solo el enlace al valor, o al valor de la variable, siempre debes de usar bindValue, a menos que quieras usar variables de entrada/salida.

Saludos.
  #5 (permalink)  
Antiguo 23/01/2012, 10:24
Avatar de RIVERMILLOS  
Fecha de Ingreso: marzo-2010
Mensajes: 1.336
Antigüedad: 14 años, 1 mes
Puntos: 15
Respuesta: problema con pdo e insert y parametro

Ok entonces debo usar siempre bindValue , pero por favor una ejemplo de lo de bindParam no me quedo claro lo de las variable de entra y salida?


slaudos
  #6 (permalink)  
Antiguo 23/01/2012, 11:08
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: problema con pdo e insert y parametro

Por ejemplo cuando tienes un StoredProcedure que devuelve variables, puedes usar bindParam para enlazar los valores que devuelve tu StoredProcedure a variables de PHP.

Saludos.
  #7 (permalink)  
Antiguo 23/01/2012, 11:17
Avatar de RIVERMILLOS  
Fecha de Ingreso: marzo-2010
Mensajes: 1.336
Antigüedad: 14 años, 1 mes
Puntos: 15
Respuesta: problema con pdo e insert y parametro

Cita:
Iniciado por GatorV Ver Mensaje
Por ejemplo cuando tienes un StoredProcedure que devuelve variables, puedes usar bindParam para enlazar los valores que devuelve tu StoredProcedure a variables de PHP.

Saludos.
Muchas gracias , voy a cambiar todo a bindValues ,es necesario colocar el ultimo parámetro de esta función?


saludos
  #8 (permalink)  
Antiguo 23/01/2012, 11:31
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: problema con pdo e insert y parametro

Sí, para que le indiques a PDO como debe de tratar el valor.

Saludos.

Etiquetas: insert, parametro, pdo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:18.