Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. <?php
  2. class with_DB {
  3.     public $host = 'localhost';
  4.     public $username = 'root';
  5.     public $password = "";
  6.     public $db = 'testdb';
  7.    
  8.     public function connect_DB() {
  9.         $link = mysql_connect($this->host, $this->username, $this->password);
  10.         if(!link) {
  11.             die( "Нет подключения к БД:". mysql_error());
  12.         }
  13.         mysql_select_db($this->db) or die(mysql_error());
  14.        return $link;
  15.     }
  16.  
  17.      public function write($p) { //функция записи сообщения
  18.         $sql = 'INSERT INTO messages(title, body, created, contacts) VALUES("' . $p["title"] . '" , "' . $p["body"] . '", ' . time() .', "'.$p["contacts"].'")';  
  19.         return mysql_query($sql) or die(mysql_error());
  20.     }
  21.      
  22.     public function display_public() { //функция вывода сообщений
  23.         $content = '';
  24.         $sql = 'SELECT mid, title, body  FROM messages ';
  25.         $result = mysql_query($sql) or die(mysql_error());
  26.          
  27.         while($row = mysql_fetch_array($result) or die(mysql_error())) {    
  28.             $pages = array (
  29.                 $row->mid => array("title" => $row->title, "body" => $row->body)
  30.                 );
  31.                 $page_id = $row->mid;
  32.                 if(isset($_GET['id'])) {
  33.                     $page_id = $_GET['id'];
  34.                 }
  35.                 if (!isset($pages[$page_id])) $page_id = $row->mid;
  36.                 $page  = $pages[$page_id];
  37.                 $content .= '<html>
  38. <head>
  39.         <title>'.$page[title].'</title>
  40. </head>
  41. <body>
  42.  <p><a href="?id=' . $page[mid] . '">Первая</a></p><br>
  43.  <p> '.$page[body].'</p>
  44. </body>
  45.     </html>';
  46.         }
  47.         return $content;
  48.     }
  49. }
  50. $obj = new with_DB;
  51. $db_connection = $obj->connect_DB();
  52. $item = array("title" => "заголовок", "body" => "текст", "contacts" => "номер");
  53. echo $obj->write($item);
  54. echo $obj->display_public();
  55. mysql_close($db_connection);
  56.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement