mysqli_fetch_object () expects parameter 1

5

I have a problem with my php code that sends me the following error

  

"Warning: mysqli_fetch_object () expects parameter 1 to be   mysqli_result, boolean ", This is my php code where I get an error.

<?php

            include("connection.php");
            if(isset($_POST['submit']))
            {
                if($_POST['nombre'] == '' or $_POST['apellido'] == '' or $_POST['email'] == '' or $_POST['usuario'] == '' or $_POST['pass'] == '')
                {
                    echo "Debe llenar todo los datos por favor.";
                }else{
                    $sql = 'SELECT * FROM monitores';
                    $rec = mysqli_query($conexion,$sql);
                    $verificar = 0;

                    while($resultado = mysqli_fetch_object($rec))
                    {
                        if($resultado->usuario == $_POST['usuario'])
                        {
                            $verificar = 1;
                        }
                    }
                    if($verificar == 0)
                    {
                        $nom = $_POST['nombre'];
                        $apel = $_POST['apellido'];
                        $mail = $_POST['email'];
                        $usr = $_POST['usuario'];
                        $pass = $_POST['pass'];

                        $conexion->query("INSERT INTO monitores (nombre,apellido,email,usuario,contrasena) VALUES ('$nom','$apel','$mail','$usr','$pass')");
                        mysqli_query($conexion,$sql);


                        echo ' <script language="javascript">alert("Usuario registrado con éxito");</script> ';

                    }
                }
            }



        ?>

This is what is found in "connection.php"

<?php
$host="localhost";
$hostuser="root";
$hostpw="";
$hostdb="fdj";

   $conexion = mysqli_connect($host, $hostuser, $hostpw);

if (!$conexion) {
    die('No se ha podido conectar a la base de datos');
}

?>
    
asked by user13310 25.08.2016 в 16:49
source

1 answer

1

That error occurs because the result of the query failed for some reason, and then the return value was false instead of a resultset which is the first input parameter of mysqli_fetch_object .

The first thing I would do is add some code to debug and see what error occurs. You can do this using mysqli_error . Something like this (in the query / SQL statement in which you receive the indicated error). Assuming it's the first of the two that there are:

$sql = 'SELECT * FROM monitores';
$rec = mysqli_query($conexion,$sql);
if (!$rec) {
    echo("Error: %s\n", mysqli_error($conexion));
}

That will show you the error message and from there you can solve the problem, whatever it may be. Viewing the code, it is probably something like a wrong table / column name, or the database was not selected correctly (where is it selected?), Or some syntax error (by the quotes) ...

Apart from that, as I mentioned in a comment above, you should switch to prepared statements instead of creating dynamic SQLs that can suffer SQL injection attacks.

    
answered by 25.08.2016 в 17:14