Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. <?php
  2. //Conexao.php
  3. include_once 'Registry.php';
  4.     $Conn = new PDO('pgsql:host=localhost port=5432 dbname=base user=usuario password=senha');
  5.     $Registry = Registry::getInstance();
  6.     $Registry->set( 'PDO' , $Conn );
  7.     $path = $Conn->query('SET search_path TO nome_schema;'); // SCHEMA
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.     $Conn = Registry::getInstance()->get( 'PDO' );
  21. ?>
  22.  
  23. <?php
  24. //Registry.php
  25. /**
  26.  * Exemplo de Registry
  27.  */
  28. class Registry {
  29.         /**
  30.          * Instância única de Registry
  31.          * @var Registry
  32.          */
  33.         private static $instance;
  34.  
  35.         /**
  36.          * Nosso registro
  37.          * @var ArrayObject
  38.          */
  39.         private $storage;
  40.  
  41.         /**
  42.          * Registry é um caso de uso de Singleton
  43.          */
  44.         protected function __construct() {
  45.                 $this->storage = new ArrayObject();
  46.         }
  47.  
  48.         /**
  49.          * Recupera um registro utilizando sua chave
  50.          * @param string $key
  51.          * @return mixed O valor armazenado
  52.          * @throws RuntimeException Se não houver um registro para a chave especificada
  53.          */
  54.         public function get( $key ) {
  55.                 if ( $this->storage->offsetExists( $key ) ) {
  56.                         return $this->storage->offsetGet( $key );
  57.                 } else {
  58.                         throw new RuntimeException( sprintf( 'Não existe um registro para a chave "%s".' , $key ) );
  59.                 }
  60.         }
  61.  
  62.         /**
  63.          * Recupera a instância única de Registry
  64.          * @return Registry
  65.          */
  66.         public static function getInstance() {
  67.                 if ( !self::$instance )
  68.                         self::$instance = new Registry();
  69.  
  70.                 return self::$instance;
  71.         }
  72.  
  73.         /**
  74.          * Registra um valor à uma chave
  75.          * @param string $key A chave que será utilizada no registro
  76.          * @param mixed $value O valor que será registrado
  77.          * @throws LogicException Se a chave já estiver registrada
  78.          */
  79.         public function set( $key , $value ) {
  80.                 if ( !$this->storage->offsetExists( $key ) ) {
  81.                         $this->storage->offsetSet( $key , $value );
  82.                 } else {
  83.                         throw new LogicException( sprintf( 'Já existe um registro para a chave "%s".' , $key ) );
  84.                 }
  85.         }
  86.  
  87.         /**
  88.          * Remove o registro de uma chave específica
  89.          * @param string $key A chave que será removida
  90.          * @throws RuntimeException Se não houver um registro para a chave especificada
  91.          */
  92.         public function unregister( $key ) {
  93.                 if ( $this->storage->offsetExists( $key ) ) {
  94.                         $this->storage->offsetUnset( $key );
  95.                 } else {
  96.                         throw new RuntimeException( sprintf( 'Não existe um registro para a chave "%s".' , $key ) );
  97.                 }
  98.         }
  99. }
  100. ?>
  101.  
  102. <?php
  103. //AQUI FOI A DICA DO Thiago Santos
  104. function busca_valor($parametro) {
  105. $Conn = Registry::getInstance()->get( 'PDO' );
  106.  
  107. $retorno = array()
  108.        $result = $Conn->query("SELECT * FROM tabela WHERE campo = ".$parametro."");
  109.        foreach ( $result->fetchAll( PDO::FETCH_OBJ ) as $valor ){
  110.                                   $retorno[] = $valor->campo;
  111.       }
  112. return $retorno;
  113. }
  114. ?>
  115.  
  116.  
  117. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement