Advertisement
NFL

Untitled

NFL
Mar 30th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. <?php
  2. class DB {
  3. protected $_conn = null;
  4.     public function __construct() {
  5.         //инициализируем соединение...
  6.         $link = mysql_connect('localhost', 'root', '');
  7.         $this->_conn = $link; //некая переменная, содержащая наше соединение с БД
  8.         mysql_select_db('dbname', $this->_conn);
  9.     }
  10.     public function query($sql) {
  11.         return mysql_query($sql, $this->_conn); //используем полученный линк БД
  12.     }
  13. }
  14. class Model extends DB {
  15.     //public-конструктор родителя вызывается автоматически, нам доступен ресурс parent::$_conn
  16.     public function testConn() {
  17.         $result = $this->query("SELECT * FROM table"); //выбираем из одной таблицы
  18.         var_dump(mysql_num_rows($result));
  19.     }
  20.     public function testConn2() {
  21.         $result = $this->query("SELECT * FROM table2"); //с тем же соединением - но из другой
  22.         var_dump(mysql_num_rows($result));
  23.     }
  24. }
  25. $a = new Model();
  26. $a->testConn(); //первая выборка
  27. $a->testConn2(); //вторая выборка
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement