There is a database connection class:

class Mydb { protected $db; public function __construct() { try { $this->db = new PDO('mysql:host=localhost;dbname=mydb', 'root', ''); } catch (PDOException $e) { exit($e->getMessage()); } } } 

And two successor classes: to add data to the database

 class AddData extends Mydb { public function addInfo($name, $tel, $email) { $this->db->exec("INSERT INTO users (name, tel, email) VALUES ('$name', '$tel', '$email'"); } } 

and for getting from the base

 class GetData extends Mydb { public function getInfo() { $this->db->query("SELECT * FROM users"); } } 

The object of the AddData class with its function copes perfectly:

 $add = new AddData(); $add->addInfo('Vasya', '(495) 123-45-67', 'vas@ya.ru'); 

But the object of the class GetData for some reason returns NULL:

 $get = new GetData(); $res = $get->getInfo(); var_dump($res); //NULL 

And if $ this-> db-> query ("SELECT * FROM users"); put in the constructor of the parent class, then everything works. And through the heir class - no.

What is the problem here and how to solve it?

Closed due to the fact that off-topic participants Alexey Shimansky , Ipatiev , ߊߚߤߘ , user194374, rjhdby 13 Feb '17 at 15:01 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - ߊߚߤߘ, community spirit, rjhdby
  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Alexey Shimansky, Ipatiev
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    I forgot to return in getInfo () - Maxim Stepanov
  • @ MaksimStepanov yes, it worked with return, thanks! - humster_spb
  • $add->addInfo('Vasya', '(495) 123-45-67', 'vas@ya.ru\'), (\'Запасной аккаунт\', \'\',\''); - vp_arth

1 answer 1

I do not know where they learned to write classes, what is written in the question is worse than atomic war.

"class" Mydb throw in the trash, the rest into one.

 class User { public function __construct($pdo) { $this->db = $pdo; } public function add() { $sql = "INSERT INTO users (name, tel, email) VALUES (?,?,?)"; $this->db->prepare($sql)->execute($name, $tel, $email); } public function getAll() { return $this->db->query("SELECT * FROM users")->fetchAll(); } } 

use so

 $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', '', [3 => 2]); $user = new User($pdo); $res = $user->getAll(); var_dump($res); //NULL 
  • And how bad is that each class is responsible for one specific action? One for the connection, the other for the insert, the third for the select, and so on. Why do we need one class to load all these functions? - humster_spb
  • Well, you read at least something about the classes. what it is and why they are needed. And what is different from the functions. - Ipatiev
  • Although in fact you are right - in this case, you should not do the class, but simply pile up a bunch of functions. it will be much better to reflect your "architecture" - Ipatiev
  • Well, that is, you have no arguments, I understand)) - humster_spb
  • You're so cute in your aggressive ignorance :) - Ipatyev