sql >> Base de Datos >  >> RDS >> Mysql

Conexión a AWS RDS a través de PDO

Tengo el código funcionando ahora, aunque, lamentablemente, nunca llegué al fondo de por qué no funcionaba en primer lugar. Sospecho que tuvo algo que ver con no detectar el número de puerto correctamente, tal vez un error tipográfico en algún lugar que se corrigió 'accidentalmente' (en lugar de deliberadamente) cuando estaba probando las cosas. Este código ahora funciona (solo para MySQL):

      $dsn = null;
      $options = null;
      $this->host = SYSTEM_CONFIG["database"]["host"];
      $this->type = SYSTEM_CONFIG["database"]["type"];
      $this->name = SYSTEM_CONFIG["database"]["name"];
      $this->user = SYSTEM_CONFIG["database"]["user"];
      $this->pass = SYSTEM_CONFIG["database"]["pass"];
      $this->port = SYSTEM_CONFIG["database"]["port"];

      switch ($this->type) {
         case "SQLSRV":
            // Other untested code...
            break;
         default: 
            $dsn = "mysql:host={$this->host};port={$this->port};dbname={$this->name};charset=utf8";
            $options = [
               PDO::ATTR_PERSISTENT => false,
               PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
               PDO::ATTR_EMULATE_PREPARES => false,
               PDO::ATTR_STRINGIFY_FETCHES => false
            ];
      }
      try {
         $this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
      } catch (PDOException $e) {
         $this->logError($e);
      } catch (Exception $e) {
         $this->logError($e);
      }