I study OOP.

This is how I usually connect to the database:

<?php $db = mysql_connect('localhost', 'user', 'password'); mysql_select_db('bdname', $db); $result = mysql_query('SELECT * FROM таблица WHERE id=2', $db); $row = mysql_fetch_array($result); echo $row[image]; ?> 

Tell me how to connect to the database to the table using OOP?

In the internet I was looking for educational materials on OOP and MYSQL in php, I could not find it.

If so, skip the links.

    4 answers 4

    Database information file, for example db-info.php

     $server = "localhost"; $user = "admin"; $pass = "demo"; $database = "my_database"; 

    The page to which we want to connect the database, for example index.php

     include("db-info.php"); $link = mysql_connect($server, $user, $pass); if(!mysql_select_db($database)) die(mysql_error()); 

    All now we can on the index.php page make any request to the database, for example, get all the information about users

     $result = mysql_query("SELECT * FROM `users`"); $r = mysql_fetch_array($result); $name = $r['name']; 

    Now the $ name variable will select the first username of any user from the users table

    Well, so it would all look

     <?php include("db-info.php"); $link = mysql_connect($server, $user, $pass); if(!mysql_select_db($database)) die(mysql_error()); $result = mysql_query("SELECT * FROM `users`"); $r = mysql_fetch_array($result); $name = $r['name']; ?> <html> <head> //сдесь содержимое хэдэра </head> <body> <p><?php echo $name; ?></p> </body> </html> 

    Well, of course it is not necessary to write the whole PHP script to the top of the page and you can do it more carefully, for example, via include ("file.php");

    • die(mysql_error()) is a bad practice for production. - Oleg Arkhipov
    • one
      Why bad ??? In general, it is possible to display in the place of mysql_error just an error message, such as "database is not available." - webkostya
    • @Berserk, it is better to either attach to try-catch , or throw an exception and catch it just as a global handler. So more flexible in customizing the display of errors. - Oleg Arkhipov
    • one
      And you can primerchik? - webkostya

    First, we learn PHP: PDO , then when we penetrated and realized that it’s not very convenient and a lot of repetitive code, google: "PDO wrapper" or "PDO wrapper", then we comprehend zen and write our wrapper.

      Class:

       class Base { function conn() { // подключение к БД mysql_connect('localhost','user','password'); mysql_select_db('bdname'); } } 

      Work with DB:

       $object = new Base(); // создаем объект $object->conn(); // подключаемся к БД $result = mysql_query('SELECT * FROM таблица WHERE id=2 '); $row = mysql_fetch_array($result); echo $row[image]; 
      • 3
        it's not even funny. - FLK
      • The last three lines are not OOP. - Andrei2
      • @ Andrey2 Are you seriously considering writing wrappers for mysql_ * functions? - FLK

      This is not the PLO at all. You need to create a constructor and destructor, here is an example from the working code:

       class main { function __construct() { //Создаем свойство содержащее объект MySQLi $this->mysqli = new mysqli($host, $user, $password, $database, $port, $socket); } //Некий метод public function someMethod() { //Пример использования (через свойство) $sql = "SELECT `date_add`, `date_out` FROM `vkb_bans_ip` WHERE `active`='1' AND `ip`=?"; $query = $this->mysqli->prepare($sql); $query->bind_param('s', $ip); $query->execute(); } function __destruct() { $mysqli->close(); } }