Referenced method is not found in subject class in PHPSTORM

-3

In my code, in PHPSTORM it returns that method fetch_all was not found.

<?php
$conn = require 'connection.php';

$result = $conn->query('SELECT * FROM users');

$users = $result->fetch_all(MYSQLI_ASSOC);

?>

As I've just started, this is still the only code on the page outside HTML.

And this is the error it shows in the browser:

Fatal error: Uncaught Error: Call to a member function query() on integer in C:\Users\Administrador\Documents\estudosphp\crud\index.php:4

Code of the connection.php page:

$conn = new mysqli('localhost', 'root', '', 'php_mysql');

if($conn-> connect_errno){
    die('falhou a conexão ('. $conn -> connect_errno .')' . $conn->connect_error);
}
    
asked by anonymous 04.09.2018 / 05:04

1 answer

1

You did:

$conn = require 'connection.php';

But the connection.php file has no return, so its $conn variable will not be what you expect to be. The ideal, in my opinion, is you correctly define the return in your file:

<?php  // connection.php

$conn = new mysqli('localhost', 'root', '', 'php_mysql');

if($conn-> connect_errno){
    die('falhou a conexão ('. $conn -> connect_errno .')' . $conn->connect_error);
}

return $conn;

Or, simply get the assignment in require , since you are already setting the variable $conn within connection.php

<?php
require 'connection.php';

$result = $conn->query('SELECT * FROM users');

$users = $result->fetch_all(MYSQLI_ASSOC);

See the documentation :

  

Manipulating Returns: include returns FALSE upon failure and issues a warning. Successful inclusions, unless overridden by the included file, return 1 . You can use the return statement inside the included file to finish processing and return to the file that included it.

That is, you are trying to call the fetch_all method of an integer 1.

    
04.09.2018 / 14:29