I study OOP in php . I wrote a simple class for connecting to the database and creating a table. I do not cut why it does not work, I understand that there is no connection to the database in the $link= $this->mysql_connect_djo(); but I do not understand why. Poke my nose knowing, please! Here is the code

 <?php $host = "localhost"; $user = "test"; $password = "123456"; $database = "class2"; $query = "CREATE TABLE notepad_info ( id INT PRIMARY KEY AUTO_INCREMENT, fio VARCHAR(60) NOT NULL, notepad_text TEXT )"; class MYSQL_RABOTA { //ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌΡΡ ΠΊ Π±Π°Π·Π΅ protected function mysql_connect_djo() { global $host, $user, $password, $database; $link = mysqli_connect($host, $user, $password, $database); if (!$link) { echo "НС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒΡΡ ΠΊ Π±Π°Π·Π΅" . " " . mysqli_connect_error(); exit(); } } //Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ function create_table() { global $query; $link= $this->mysql_connect_djo(); if (mysqli_query($link, $query)) { echo "Запрос ΠΎΡ‚ΠΏΡ€Π°Π²Π»Π΅Π½"; } else { echo "Ошибка ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ запроса "; } mysqli_close($link); } } $mysql_djo = new MYSQL_RABOTA; $mysql_djo->create_table(); ?> 
  • 3
    For the student - always write exactly how something does not work: errors, exceptions, white screen, smoke from the system, etc. If the error contains a string - mark it in the source. While it is difficult to understand why somewhere something somehow does not work) - Sh4dow
  • one
    It is not necessary to use global in the class, describe the logins / passwords in the constructor, and put the parameters to create the table in the create_table method, put $ link in $ this-> link; right in the mysql_connect_djo method. as a result, the call should be something like this: $ mysql_djo = new MYSQL_RABOTA ("test", "123456", "class2"); $ mysql_djo-> create_table ("notepad_info", "id INT PRIMARY KEY AUTO_INCREMENT", "fio VARCHAR (60) NOT NULL", "notepad_text TEXT") - zb '10
  • well, use the mysqli object interface to not switch back and forth. ( php.net/manual/ru/mysqli.quickstart.dual-interface.php ) - zb '10
  • one
    Here [here] [1] can see an example. [1]: vazelin.org.ua/archives/342/izuchaem-oop-na-php-urok-1 - Indev
  • bad lesson - zb '

2 answers 2

You do not pass the parameters! In general, I would change the class so:

  class MYSQL_RABOTA { private host, user,password, database, link; //Π”ΠΎΠ±Π°Π²Π»Π΅Π½Π° строка; function __construct($host, $user, $password, $database) //Π”ΠΎΠ±Π°Π²Π»Π΅Π½Π° функция; { $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; } protected function mysql_connect_djo() { //global $host, $user, $password, $database; Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΡƒΠ΄Π°Π»Π΅Π½Π°; $this->link = mysqli_connect($this->host, $this->user, $this->password, $this->database); //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½Π°; if (!$this->link) { echo "НС Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒΡΡ ΠΊ Π±Π°Π·Π΅" . " " . mysqli_connect_error(); exit(); } return ($this->link); //Π‘Ρ‚Ρ€ΠΎΠΊΠ° Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π°; } //Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ function create_table($query) { //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½Π°; //global $query; Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΡƒΠ΄Π°Π»Π΅Π½Π°; $this->mysql_connect_djo(); //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½Π°; if (mysqli_query($this->link, $query)) { //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½Π°; echo "Запрос ΠΎΡ‚ΠΏΡ€Π°Π²Π»Π΅Π½"; } else { echo "Ошибка ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ запроса "; } mysqli_close($this->link); //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½Π°; } } $mysql_djo = new MYSQL_RABOTA($host, $user, $password, $database); //Π‘Ρ‚Ρ€ΠΎΠΊΠ° ΠΎΡ‚Ρ€Π΅Π΄Π°ΠΊΡ‚ΠΈΡ€ΠΎΠ²Π°Π½Π°; $mysql_djo->create_table($query); ?> 

It seems so. By the way, since you are already writing object-oriented, it makes sense to replace the procedural inserts

 mysqli_query($this->link, $query) Π½Π° $this->link->query($query); mysqli_close($this->link) Π½Π° $this->link->close(); 

And you can also:

 if (mysqli_query($this->link, $query)) { echo "Запрос ΠΎΡ‚ΠΏΡ€Π°Π²Π»Π΅Π½"; } else { echo "Ошибка ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ запроса "; } //Π—Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π½Π°: mysqli_query($this->link, $query) or exit("Ошибка ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠΈ запроса"); echo "Запрос ΠΎΡ‚ΠΏΡ€Π°Π²Π»Π΅Π½"; 

    One:
    Your connection function returns nothing. And it would be nice, at least to return the link.

    Two:
    start learning OOP with global - a bad omen. =) Especially for the purpose for which you use.

    It is better:

     class my_class{ const db_name="db1"; const db_pass="db2"; 

    Or even in the configuration file.