Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- protected $_conn = null;
- public function __construct() {
- //инициализируем соединение...
- $link = mysql_connect('localhost', 'root', '');
- $this->_conn = $link; //некая переменная, содержащая наше соединение с БД
- mysql_select_db('dbname', $this->_conn);
- }
- public function query($sql) {
- return mysql_query($sql, $this->_conn); //используем полученный линк БД
- }
- }
- class Model extends DB {
- //public-конструктор родителя вызывается автоматически, нам доступен ресурс parent::$_conn
- public function testConn() {
- $result = $this->query("SELECT * FROM table"); //выбираем из одной таблицы
- var_dump(mysql_num_rows($result));
- }
- public function testConn2() {
- $result = $this->query("SELECT * FROM table2"); //с тем же соединением - но из другой
- var_dump(mysql_num_rows($result));
- }
- }
- $a = new Model();
- $a->testConn(); //первая выборка
- $a->testConn2(); //вторая выборка
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement