Ver Mensaje Individual
  #4 (permalink)  
Antiguo 25/11/2013, 11:36
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: Error Mysqli sentencia preparada con un if simple

No, hay caso, si pongo comillas simples el error es identico:

Cita:
SELECT if(articulos.stock>0,'si','no') FROM articulos;)You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''si','no') FROM articulos' at line 1
en phpmyadmin anda bien de ambas formas, asi que no creo que sea un problema de la consulta en si, sino de cuando se prepara.
la clase que uso tiene estos 3 metodos y falla en _prepararSentencia()

la variable $sentencia que recibe el metodo rawQuery es la consulta con el if
Código PHP:
Ver original
  1. protected function rawQuery($sentencia, $bindParametros = NULL)
  2.     {
  3.         $this->_sentencia = filter_var($sentencia, FILTER_SANITIZE_STRING);
  4.         $stmt = $this->_prepararSentencia();
  5.  
  6.         if (is_array($bindParametros) === true)
  7.         {
  8.             $parametros = array(''); // Crea vacio el indice 0.
  9.             foreach ($bindParametros as $campo => $valor)
  10.             {
  11.                 $parametros[0] .= $this->_determinarTipo($valor);
  12.                 $parametros[] = $valor;
  13.             }
  14.  
  15.             call_user_func_array(array($stmt, 'bind_param'), $this->referenciarValores($parametros));
  16.         }
  17.  
  18.         $this->ejecutarSentencias($stmt);
  19.         return $this->_bindResultados($stmt);
  20.     }
  21.  
  22. protected function _prepararSentencia()
  23.     {
  24.         if (!$stmt = $this->_mysqli->prepare($this->_sentencia))
  25.             echo('Ocurrio un error preparando la sentencia ('.$this->_sentencia.') '.$this->_mysqli->error);
  26.  
  27.         return $stmt;
  28.     }
  29.  
  30. private function ejecutarSentencias(mysqli_stmt &$stmt)
  31.     {
  32.         if(!$stmt->execute())
  33.         {
  34.             if($this->_explotar)
  35.                 die('Ocurrio un error procesando los datos ('.$this->_mysqli->errno.') '.$this->_mysqli->error);
  36.             else {
  37.                 $this->_error = $this->_mysqli->error;
  38.                 $this->_nroError = $this->_mysqli->errno;
  39.             }
  40.         }
  41.     }
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios