Referenced method is not found in Subject class in PHPSTORM

Asked

Viewed 49 times

-3

In my code, in PHPSTORM 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 just started, this is still the only code on the page, apart from HTML.

And this is the error that 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 for 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);
}
  • Post the code for "Connection.php", please

  • I posted the code.

  • Have you tried fetchAll()?

  • Yes, the result is the same.

  • fetchAll() is for PDO, which is not the case...

  • Use the fetch_assoc straightforward

  • If $conn receives the require, but the file connection.php returns nothing, what should be $conn?

  • Gives a print_r($conn)

  • Change the $conn = require 'connection.php';&#xA; for require 'connection.php'; or require_once 'connection.php';

Show 4 more comments

1 answer

1


You did:

$conn = require 'connection.php';

But the file connection.php has no return, so its variable $conn will not be what you expect to be. Ideally, in my view, is you correctly set the return on 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 take the assignment on require, since you are already defining the variable $conn inside 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 on failure and sends a warning. Successful inclusions, unless overwritten by the included file, return 1. You can use the Return declaration inside the included file to finish processing and return to the file that included it.

I mean, you’re trying to call the method fetch_all of an integer 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.