As a solution Catchable fatal error: Object of class mysqli_result could not be converted to string

-1

I want to show the name of him I have this code:

<?php
session_start();
$_SESSION['name']  = $_POST['usuario'];

$usuario = $_POST['usuario'];
$pass = $_POST['contra'];

if(empty($usuario) || empty($pass)){
        echo"<script type=\"text/javascript\">alert('NO deje campos vacios'); window.location='Login.html';</script>";

exit();

}

$conexion=mysqli_connect("localhost","root","","reservar");
$consulta1="Select nombre from usuarios where Correo='$usuario' and Pass='$pass'";
$consulta="Select * from usuarios where Correo='$usuario' and Pass='$pass'";
$resultado=mysqli_query($conexion,$consulta);
$resultado1=mysqli_query($conexion,$consulta1);


$_SESSION['name1'] =$resultado1;
$filas=mysqli_num_rows($resultado);

if ($filas>0){

    header("location:home.php");



}else{


    echo"<script type=\"text/javascript\">alert('Usuario o contraseña incorrecta'); window.location='Login.html';</script>";

}



mysqli_free_result($resultado);

mysqli_close($conexion);
?>
    
asked by Carlos Mendez 22.06.2017 в 20:21
source

1 answer

1

The error is quite clear, the variable you try to assign to the name1 key of $_SESSION is type Result .

To be able to assign you will have to obtain the individual values of the query, there are many ways like fetch_array . Also keep in mind that you only need a query for login .

Final Code but not the best option

$conexion=mysqli_connect("localhost","root","","reservar");
/* Si desea obtener el nombre y correo sería , teniendo en cuenta el nombre de las tablas */
$consulta="Select nombre,correo from usuarios where Correo='$usuario' and Pass='$pass'";
$resultado=mysqli_query($conexion,$consulta);
/* Si Hay Resultados*/
if(mysqli_num_rows($resultado)>0){
    $fila = $resultado->fetch_array(MYSQLI_ASSOC);
    /* Asignamos A Sessión el valor de la columna Name*/
    $_SESSION['name']= $fila['nombre '];
    $_SESSION['correo']= $fila['correo'];
    header("location:home.php");
}
else{
    echo "NO HAY RESULTADOS";
}
  

As a recommendation, el concatenar los valores es una muy mala practica , you should review a Related question with this topic and optar por usar desde ya consultas preparadas .

Update Using PDO to handle prepared queries.

try { 
 /* Creamos La Conexión con PDO, modificar los valores respectivos*/
    $bd = new PDO('mysql:host=localhost;dbname=reservar',"root", "",array(PDO::ATTR_PERSISTENT => true));
    $bd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sentencia = $bd->prepare("Select nombre,correo from usuarios where Correo=:user and Pass= :pass");
    $sentencia->bindValue(':user', $usuario, PDO::PARAM_STR);
    $sentencia->bindValue(':pass', $pass, PDO::PARAM_STR);
    $sentencia->execute();
    if($sentencia->rowCount()>1){
        $_SESSION['name']= $fila['nombre '];
        $_SESSION['correo']= $fila['correo'];
        header("location:home.php");
    }
    else
        echo "NO HAY RESULTADOS";
}
catch (Exception $e) {
     /* Cancelamos La Transacción por si exista Error*/
    $bd->rollBack();
    echo "Se Presento Un Error :  " . $e->getMessage();
}
    
answered by 22.06.2017 / 20:37
source