Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/11/2017, 14:03
stevens82
 
Fecha de Ingreso: octubre-2011
Mensajes: 132
Antigüedad: 12 años, 6 meses
Puntos: 4
Respuesta: Error al conectar a la base de datos con PDO y variable

prueba esto:

Código SQL:
Ver original
  1. $host = 'localhost';
  2.         $db = 'nombre_db';
  3.         $dsn = "mysql:host = $host; dbname = $db";
  4.         $user = 'root';
  5.         $pass = '123456';
  6.  
  7.         //Opciones de PDO.
  8.         $options = array(   PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
  9.                             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
  10.  
  11.         try {
  12.             $conexion = NEW PDO($dsn, $user, $pass, $options);
  13.             RETURN $conexion;
  14.         }
  15.         catch (PDOException $e) {
  16.             //echo "Sucedio un problema al realizar la conexión !!";
  17.             echo "¡Error!: " . $e->getMessage() . "";
  18.             die();
  19.             exit();
  20.         } finally {
  21.             $conexion = NULL;
  22.         }