How to resolve "Function name must be a string" error?

Asked

Viewed 3,654 times

-2

Fatal error: Uncaught Error: Function name must be a string in C: xampp htdocs Proj_solos php login.php:11 Stack trace: #0 {main} thrown in C: xampp htdocs Proj_solos php login.php on line 11

<?php include_once './conexao.php';
$erro= array();
 if (isset($_POST['login']) && strlen($_POST['login']) >0 ){
  if (!isset($_SESSION))
      session_start ();

  $_SESSION['login'] = $mysqli ->escape_string($_POST['login']);
  $_SESSION['senha'] = md5(md5($_POST['senha'])); 

  $sql_code = "SELECT senha,login FROM usuario ->' $_SESSION[login]'";
  $sql_query = $mysqli($sql_code) or die ($mysqli->erro);
  $dado= $sql_query -> fetch_assoc();
  $total = $sql_query -> num_rows;

  if($total ==0){
      $erro[] = "Este Usuario não existe.";
  }else{
      if($dado['senha'] == $_SESSION['senha']){
          $_SESSION['login'] = $dado["ID"];

      }else{
          $erro[]= "senha Incorreta!";
      } 
  }
   if (count($erro ==0 || !isset($erro))){
       echo "<script>alert('Login efetuado com Sucesso');location.href='../php/index.php';</script>";
   }
 }
?>
  • 3

    Error: Function name must be a string in login.php (11). That’s what’s wrong. Look at line 11 of the file login.php and tell me what you did...

  • 1

    It is not to say what is wrong. We realized that you did not identify, otherwise you would not have created the question. It’s to explain what you did on this line.

2 answers

2


The wrong line clearly is:

$sql_query = $mysqli($sql_code) or die ($mysqli->error);

The error message has also been corrected, from $mysqli->erro for $mysqli->error.

Let’s consider that $mysqli be the object of connection to the database and the same was created in the file conexao.php. If this premise is wrong, nothing in your code makes sense. If it is right, in the above line you are passing the value of $sql_code for the object $mysqli:

$mysqli($sql_code)

This makes no sense. For you to execute an SQL command, you need to invoke the method query of this object. Assuming you are using the OOP syntax of Mysqli, it would look something like:

$mysqli->query($sql_code)

This would solve the current error, but generate another one, because your SQL command is wrong:

SELECT senha,login FROM usuario ->' $_SESSION[login]'

You used this arrow syntax that does not exist in SQL. Probably what you wanted to do is:

SELECT senha, login FROM usuario WHERE login = '{$_SESSION[login]}'

Notice the use of the clause WHERE, the identification of the column login and the use of the operator =.


The last condition of the code also makes no sense:

if (count($erro ==0 || !isset($erro))) { ... }

You are passing the return of the operation x || y, which will be of the boolean type, for the function count. Do you want to count the elements of a boolean? No. Probably what you wanted to do is:

if ((count($erro) == 0) || !isset($erro)) { ... }

But also there is no need to do. The second operand will verify the non-existence of the variable $erro, but it is defined in line 2, that is, it will always exist. So, !isset($erro) will always return False, regardless of the value of $erro. To check if there is any error message, just the count:

if (count($error) == 0) { ... }
  • obg I changed even more this giving the error I will redo my code because probably the error should be in the connection.php

  • 1

    @Jeanguilherme the error message tells you exactly what is wrong, no need to keep trying to guess.

  • 2

    Is there a way to vote again? xD Excellent explanation !!

-2

$mysqli is as variable. It must be a command, ie without the $ (dollar sign). Only mysqli.

  • i pulled from 1 mysqli and this new error: Fatal error: Uncaught Error: Call to Undefined Function mysqli() in C: xampp htdocs Proj_solos php login.php:11 Stack trace: #0 {main} thrown in C: xampp htdocs Proj_solos php login.php on line 11

  • @Andersoncarloswoss ,my application is to register and login and when I configure the $sql_query variable that is to get the $sql_code value of this error

  • 1

    Dude, it’s like @Andrébail meant, you’re putting your variable inside the string, you have to concatenate the value and form the query. Exchange user ->' $_SESSION[login]' for WHERE usuario = '". $_SESSION[login]."'

  • Exactly as @Marcosmarques said, the point ( . ) serves to concatenate.

Browser other questions tagged

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