Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.35 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.     $Conn = Registry::getInstance()->get( 'PDO' );
  9. ?>
  10.  
  11. <?php
  12. //Registry.php
  13. /**
  14.  * Exemplo de Registry
  15.  */
  16. class Registry {
  17.         /**
  18.          * Instância única de Registry
  19.          * @var Registry
  20.          */
  21.         private static $instance;
  22.  
  23.         /**
  24.          * Nosso registro
  25.          * @var ArrayObject
  26.          */
  27.         private $storage;
  28.  
  29.         /**
  30.          * Registry é um caso de uso de Singleton
  31.          */
  32.         protected function __construct() {
  33.                 $this->storage = new ArrayObject();
  34.         }
  35.  
  36.         /**
  37.          * Recupera um registro utilizando sua chave
  38.          * @param string $key
  39.          * @return mixed O valor armazenado
  40.          * @throws RuntimeException Se não houver um registro para a chave especificada
  41.          */
  42.         public function get( $key ) {
  43.                 if ( $this->storage->offsetExists( $key ) ) {
  44.                         return $this->storage->offsetGet( $key );
  45.                 } else {
  46.                         throw new RuntimeException( sprintf( 'Não existe um registro para a chave "%s".' , $key ) );
  47.                 }
  48.         }
  49.  
  50.         /**
  51.          * Recupera a instância única de Registry
  52.          * @return Registry
  53.          */
  54.         public static function getInstance() {
  55.                 if ( !self::$instance )
  56.                         self::$instance = new Registry();
  57.  
  58.                 return self::$instance;
  59.         }
  60.  
  61.         /**
  62.          * Registra um valor à uma chave
  63.          * @param string $key A chave que será utilizada no registro
  64.          * @param mixed $value O valor que será registrado
  65.          * @throws LogicException Se a chave já estiver registrada
  66.          */
  67.         public function set( $key , $value ) {
  68.                 if ( !$this->storage->offsetExists( $key ) ) {
  69.                         $this->storage->offsetSet( $key , $value );
  70.                 } else {
  71.                         throw new LogicException( sprintf( 'Já existe um registro para a chave "%s".' , $key ) );
  72.                 }
  73.         }
  74.  
  75.         /**
  76.          * Remove o registro de uma chave específica
  77.          * @param string $key A chave que será removida
  78.          * @throws RuntimeException Se não houver um registro para a chave especificada
  79.          */
  80.         public function unregister( $key ) {
  81.                 if ( $this->storage->offsetExists( $key ) ) {
  82.                         $this->storage->offsetUnset( $key );
  83.                 } else {
  84.                         throw new RuntimeException( sprintf( 'Não existe um registro para a chave "%s".' , $key ) );
  85.                 }
  86.         }
  87. }
  88. ?>
  89.  
  90.  
  91. <?php
  92. //AQUI FOI A DICA DO Thiago Santos
  93. function busca_valor($parametro) {
  94. global $Conn;
  95. $retorno = array()
  96.        $result = $Conn->query("SELECT * FROM tabela WHERE campo = ".$parametro."");
  97.        foreach ( $result->fetchAll( PDO::FETCH_OBJ ) as $valor ){
  98.                                   $retorno[] = $valor->campo;
  99.       }
  100. return $retorno;
  101. }
  102. ?>
  103.  
  104.  
  105. <?php
  106. //DICA DO Erick
  107.  
  108. function busca_valor($parametro) {
  109. global $conn;
  110. $retorno = array()
  111. $result = $conn->query("SELECT * FROM tabela WHERE campo = ".$parametro."");
  112. $tmp = $result->fetchAll( PDO::FETCH_OBJ );
  113.        foreach ($tmp as $valor ){
  114.                     $retorno[] = $valor->campo;
  115.        }
  116.  
  117. unset($tmp);
  118. return $retorno;
  119. }
  120. ?>
  121.  
  122.  
  123. <?php
  124. //DICA DO Rodrigo Sartori Jarouche
  125. Class conexao
  126. {
  127. Protected $conn;
  128.  
  129. function  _construct($bd)
  130. {
  131.  
  132.      this->$conn = $bd
  133.  
  134. }
  135.  
  136. function  _construct($user,$pass,$host,$db) //outra assinatura passando info para conectar
  137. {
  138.  
  139.     this->$conn = New PDO('mysql:host='.$host.';dbname='.$db,$user,$pass);
  140.  
  141. }
  142.  
  143. function busca_valor($parametro)
  144. $retorno = array()
  145.  
  146. $result = $conn->query("SELECT * FROM tabela WHERE campo = ".$parametro."");
  147.  
  148. $tmp = $result->fetchAll( PDO::FETCH_OBJ );
  149.        foreach ($tmp as $valor ){                     // não enetendi o porque desse foreach, já que vc já tem o array na isntrução anterior...
  150.  
  151.                     $retorno[] = $valor->campo;
  152.        }
  153.  
  154. unset($tmp);
  155. return $retorno;
  156.  
  157. }
  158. }
  159. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement