Hello everyone, doing a task on the list of students, there is a method in the class that returns an array with all the entries.

Here is the method:

static function getStudentList(){ $db = Database::connect(); $sql = "SELECT id, name, surname, class, points FROM student" . " ORDER BY id DESC"; $result = $db->query($sql); $studentList = []; $i = 0; while($row = $result->fetch()){ $studentList[$i]['id'] = $row['id']; $studentList[$i]['name'] = $row['name']; $studentList[$i]['surname'] = $row['surname']; $studentList[$i]['class'] = $row['class']; $studentList[$i]['points'] = $row['points']; $i ++; } return $studentList; } 

Then I call it and pass it to the view where I print each line in turn.

 function indexAction($view) { $studentList = Student::getStudentList(); include_once ROOT . '/views/template.php'; } 

Everything works, but when I want to use this method in another place or even if I do so. That is, I will try to take an array and write it into another variable. As below.

  function indexAction($view) { $studentList = Student::getStudentList(); $studentListTest = Student::getStudentList(); include_once ROOT . '/views/template.php'; } 

Connection code to the database:

 static function connect(){ $dbConfig = include_once ROOT . '/config/dbConfig.php'; $dsn = "mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']}"; $user = $dbConfig['user']; $pass = $dbConfig['pass']; $db = new PDO($dsn, $user, $pass); $db->exec("set names utf8"); return $db; } 

Then i get an error

Fatal error: Call to a member function fetch () on boolean.

Please explain the reason?

  • It looks like $db->query can return false . (If the Database wrapper for PDO, then this is quite a regular situation.) - Dmitriy Simushev
  • And why is the first time all right, and the second we get false? How to fix this error? - lupti_du
  • drive.google.com/open?id=0B6hjS4ldJ_TyNE1fVmZUTUpEZ0U error.log file. Connect to the database added to the main post. - lupti_du
  • And why instead of while not using a foreach loop, the code will look nicer and easier to execute. - Shuhratjon Jumaev
  • Even with this PDO, as everything is not transparent ... it seems to me this is a jumble ... it is more logical for the connection to always return the call as we say in the same erlag either {connection_, DATA} or {connection_Error} ERROR} intercepts the try case and everything . even at the level of the final product, we immediately prescribe what to do with such an error and that’s all ... it’s extremely difficult to cause the script to fall ... (let's say that on the main page of the site you can see the fatal error), even if the database fell => call os : cmd (["etc / init.d / db start"]) of course, you should first consider the status ... - Alexander

2 answers 2

The connect () function should fix the string

 $db = new PDO($dsn, $user, $pass); 

on

 $db = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); 

In this case, errors connecting to the database will cause an exception and you will see them immediately. In the meantime, the query () method returns false and the drop already occurs on the fetch () method.

Perhaps you should not include such a mode in production, but when developing such an approach is very useful and is consistent with the rule that you need to “fall” as quickly as possible.

  • Got PDOException exception: SQLSTATE [3D000]: Invalid catalog name: 1046 No database selected in But I still don’t understand how to fix it and why everything is fine for the first time, and in the second one gives this one. - lupti_du
  • Most likely you do not connect a second time here this $ dbConfig = include_once ROOT file. '/config/dbConfig.php'; - cheops
  • You were right. Thank you very much. - lupti_du

You cannot use $result->fetch() while conditions because as far as I know you cannot use a function as a condition for while , if and so on. Try writing $result->fetch() to a separate variable.

  • one
    You are wrong, you can use functions as a condition - Dmitriy Simushev
  • hmm means wrong. - Shuhratjon Jumaev