Advertisement
Guest User

Untitled

a guest
Jan 27th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.69 KB | None | 0 0
  1. <?php
  2. class Resource
  3. {
  4.     private $resource;
  5.    
  6.     function __construct( $res )
  7.     {
  8.         $this->resource=$res;
  9.     }
  10.    
  11.     function fetch()
  12.     {
  13. //    return pg_fetch_assoc( $this->resource );
  14.  
  15.     return mysqli_fetch_assoc( $this->resource );
  16.     }
  17.    
  18.     function __destruct()
  19.     {
  20. //    pg_free_result( $this->resource );
  21.  
  22.     mysqli_free_result( $this->resource );
  23.     }
  24. }
  25.  
  26. class Database
  27. {
  28.     private $connection;
  29.    
  30.     public function __construct( $configfile='database_config.php' )
  31.     {
  32.         if ( !file_exists( $configfile ) ) throw new Exception( "Brak pliku konfiguracyjnego" );
  33.         require_once( $configfile );
  34. //          $this->connection = pg_connect( "host=$server dbname=$database user=$user password=$password" );
  35. //          if ( !is_resource( $this->connection ) ) throw new Exception( pg_last_error( $this->connection ) );
  36.  
  37.           $this->connection = mysqli_connect( $server, $user, $password, $database );
  38.           if ( !is_object( $this->connection ) ) throw new Exception( mysqli_error( $this->connection ) );
  39.        
  40.         /* change character set to utf8 */
  41.         printf("Initial character set: %s\n", mysqli_character_set_name($this->connection));
  42.  
  43.         if (!mysqli_set_charset($this->connection, "utf8")) {
  44.             printf("Error loading character set utf8: %s\n", mysqli_error($this->connection));
  45.             exit();
  46.         } else {
  47.             printf("Current character set: %s\n", mysqli_character_set_name($this->connection));
  48.         }
  49.     }
  50.  
  51.     function query( $query )
  52.     {
  53. //          $res=pg_query( $this->connection, $query );
  54. //          if ( !is_resource( $res ) ) throw new Exception( pg_last_error( $this->connection ) );
  55.  
  56.         $res=mysqli_query( $this->connection, $query );
  57.           if ( !is_object( $res ) ) throw new Exception( mysqli_error( $this->connection ) );
  58.           return new Resource( $res );
  59.     }
  60.  
  61.     function __destruct()
  62.     {
  63. //    pg_close( $this->connection );
  64.  
  65.     mysqli_close( $this->connection );
  66.     }
  67. }
  68. ?>
  69. <!DOCTYPE html>
  70. <html>
  71. <head>
  72. <title>Tworzenie tabeli</title>
  73. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  74. </head>
  75.  
  76. <body>
  77.  
  78. Dane tej tabeli:
  79. <table border="1">
  80. <?php
  81.     try
  82.     {
  83.       $db=new Database();
  84.       $result=$db->query( "select * from osoba" );
  85.       while ( $row=$result->fetch() )
  86.       {
  87.         echo "<tr>";
  88.         foreach ( $row as $col ) echo "<td>$col</td>";
  89.         echo "</tr>";      
  90.       }
  91.       $db=null;
  92.     }
  93.     catch ( Exception $e )
  94.     {
  95.         echo $e->getMessage();
  96.     }
  97. ?>
  98. </table>
  99. <hr><h2> Zrodlo pliku </h2>
  100. <?php
  101. show_source(__FILE__);
  102. ?>
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement