PHP - error Recoverable fatal error

0

hello everyone I'm wanting to filter a calendar by the user who started session, but this error is marking me

Recoverable fatal error: Object of class mysqli_result could not be converted to string

on line 34

This is the form that retrieves the name of the user who is in session or logged in.

$usuario = $_SESSION['user_name'];
$sql="select Nombre from login where user_name ='$usuario' ";
$result= mysqli_query($conexion,$sql) or die(mysqli_error());

and this is my line 34 where I make the calendar query to filter.

* Realizamos la consulta SQL */
$sql="select * from tareas where Auditor ='$result'";
$result= mysqli_query($conexion,$sql) or die(mysqli_error());
if(mysqli_num_rows($result)==0) die("No hay registros para mostrar");
 
/* Desplegamos cada uno de los registros dentro de una tabla */  
echo "<table border=0 cellpadding=15 cellspacing=3>";
    
asked by antonio sanchez 16.04.2018 в 19:31
source

2 answers

1

That error is because mysqli_query returns an object and you want to go through it directly, but for that you must Call it another way, this is the code:

if($result->mysqli_num_rows($result)==0) die("No hay registros para mostrar");

Notice that $result is your variable and as it is an object you access its function with the composite symbol ->

I think you should look at the documentation and see how the whole connection is handled, for example, it is called according to the documentation:

    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* comprobar la conexión */
if ($mysqli->connect_errno) {
    printf("Falló la conexión: %s\n", $mysqli->connect_error);
    exit();
}

The documentation will help you very much.

    
answered by 16.04.2018 / 20:12
source
0

The problem

Antonio, the problem is in the WHERE of your second query, since in it you are using the variable $result obtained previously:

$sql="select * from tareas where Auditor ='$result'";

and it turns out that this variable is not a value, but a mysqli_result object, hence the error message:

  

Recoverable fatal error: Object of class mysqli_result could not be   converted to string

The solution

You should recover a valid value to pass it in WHERE , for example in this way:

In the 1st consultation:

  • you must use some method to store the results (here I used mysqli_fetch_assoc )
  • then you get the value of the column Nombre in the variable $nombre that you will use later in the 2nd consultation

Something like this:

$usuario = $_SESSION['user_name'];
$sql="select Nombre from login where user_name ='$usuario' ";
$result= mysqli_query($conexion,$sql) or die(mysqli_error());
$arrDatos = mysqli_fetch_assoc($result);
$nombre=$arrDatos['Nombre'];

In the 2nd consultation:

  • here the only thing you have to change is the variable $result that you used before, by the variable $nombre

Something like this:

* Realizamos la consulta SQL */
$sql="select * from tareas where Auditor ='$nombre'";
$result= mysqli_query($conexion,$sql) or die(mysqli_error());
if(mysqli_num_rows($result)==0) die("No hay registros para mostrar");

/* Desplegamos cada uno de los registros dentro de una tabla */  
echo "<table border=0 cellpadding=15 cellspacing=3>";

Note on security:

Your code is highly vulnerable to SQL injection attacks , once this problem is resolved, consider shielding your code from those types of attacks by using prepared queries. Here you will find information and several examples of how to do it.

    
answered by 16.04.2018 в 23:04