Guest User

Untitled

a guest
Aug 27th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. ------------------------------------------
  2. connect.php
  3. ------------------------------------------
  4. <?php
  5. /*****************
  6.  *
  7.  * Connect to MySQL
  8.  * Author: codekK
  9.  *
  10. *****************/
  11. class DBconfig {
  12.     private $conn = null;
  13.     private $server = "server"; // Server MySQL
  14.     private $user = "user"; // Username MySQL
  15.     private $pass = "pass"; // Password MySQL
  16.     private $db = "db"; // DB MySQL
  17.    
  18.     public function newConn(){
  19.         // Setting up connection
  20.         if(!isset($this->conn)){
  21.             try {
  22.                 $this->conn = new PDO("mysql:dbname={$this->db};host={$this->server}",$this->user,$this->pass);
  23.                 $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  24.         // Error
  25.             } catch (PDOException $e){
  26.                 return false;
  27.             }
  28.         }
  29.     return $this->conn;  
  30.     }    
  31. }
  32. ?>
  33. ------------------------------------------
  34. index.php
  35. ------------------------------------------
  36. <html>
  37.     <head>
  38.         <title>Hello</title>
  39.     </head>
  40.     <body>
  41.         <h1>Hello</h1>
  42.         <?php
  43.         require_once("connect.php");
  44.         $db = new DBconfig();
  45.         $conn = $db->newConn();
  46.  
  47.         $query = $conn->prepare("SELECT usuario FROM users_liberaciones");
  48.         while ($row = $query->fetch()):
  49.             echo $row['usuario'];
  50.         endwhile;
  51.         ?>
  52.  
  53.     </body>
  54. </html>
Add Comment
Please, Sign In to add comment