Foros del Web » Programando para Internet » PHP »

usar try catch con if else

Estas en el tema de usar try catch con if else en el foro de PHP en Foros del Web. Hola, necesito saber cual es la forma correcta de usar el try catch con un if else para unas consulats teniendo algo asi: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); ...
  #1 (permalink)  
Antiguo 14/09/2018, 11:52
 
Fecha de Ingreso: octubre-2011
Mensajes: 132
Antigüedad: 12 años, 6 meses
Puntos: 4
usar try catch con if else

Hola, necesito saber cual es la forma correcta de usar el try catch con un if else para unas consulats teniendo algo asi:

Código PHP:
Ver original
  1. public function insertarBlog($categoria, $titulo)
  2.     {  
  3.         if ($group_post) {
  4.         $sql = "INSERT INTO posts (id, id_categoria, titulo, contenido, imagen, estado, slug, id_parent, id_lang, fecha_registro, fecha_edicion, id_usuario, group_post) VALUES (null, :id_categoria, :titulo, :contenido, :imagen, 1, :slug, :id_parent, :id_lang, now(), now(), '" . Session::get('id_usuario') . "', :group_post)";
  5.         $statement = $this->_db->prepare($sql);
  6.         $statement->execute(array(
  7.             ':id_categoria' => $categoria,
  8.             ':titulo'       => $titulo,
  9.         ));
  10.             if ($statement) {
  11.                 return true;
  12.             } else {
  13.                 return false;
  14.             }
  15.         } else {
  16.             $sql = "INSERT INTO posts (id, id_categoria, titulo, )";
  17.             $statement = $this->_db->prepare($sql);
  18.             $statement->execute(array(
  19.                 ':id_categoria' => $categoria,
  20.                 ':titulo'       => $titulo,
  21.             ));
  22.            if ($statement) {
  23.                 return true;
  24.             } else {
  25.                 return false;
  26.             }
  27.         }
  28.     }
  #2 (permalink)  
Antiguo 14/09/2018, 23:09
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: usar try catch con if else

Puedes comentar con mas detalle que es lo que quieres hacer coon las excepciones?
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 17/09/2018, 09:28
 
Fecha de Ingreso: octubre-2011
Mensajes: 132
Antigüedad: 12 años, 6 meses
Puntos: 4
Respuesta: usar try catch con if else

Hola, bueno solo saber como usarla con un if else
  #4 (permalink)  
Antiguo 17/09/2018, 10:02
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 9 meses
Puntos: 379
Respuesta: usar try catch con if else

Es sencillo, puedes hacer algo como
Código PHP:
Ver original
  1. try {
  2.        if ($group_post) {
  3.         $sql = "INSERT INTO posts (id, id_categoria, titulo, contenido, imagen, estado, slug, id_parent, id_lang, fecha_registro, fecha_edicion, id_usuario, group_post) VALUES (null, :id_categoria, :titulo, :contenido, :imagen, 1, :slug, :id_parent, :id_lang, now(), now(), '" . Session::get('id_usuario') . "', :group_post)";
  4.         $statement = $this->_db->prepare($sql);
  5.         $statement->execute(array(
  6.             ':id_categoria' => $categoria,
  7.             ':titulo'       => $titulo,
  8.         ));
  9.            
  10.         } else {
  11.             $sql = "INSERT INTO posts (id, id_categoria, titulo, )";
  12.             $statement = $this->_db->prepare($sql);
  13.             $statement->execute(array(
  14.                 ':id_categoria' => $categoria,
  15.                 ':titulo'       => $titulo,
  16.             ));
  17.          
  18.         }
  19.            
  20.     } catch (PDOException $exception) {
  21.        //Manejas excepción si falla algo en el insert
  22.     } catch (Exception $exception) {
  23.         //Manejas excepción si sucede algo mas
  24.     }
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.

Etiquetas: catch, sql, usar
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 09:12.