PHP Fatal error: Call to a member function fetch_assoc () on boolean


What kind of friends I hope are all well, I have a doubt working a project locally (wampserver) everything works correctly, but when uploading it to a hosting it shows me the following error message.

PHP Fatal error: Call to a member function fetch_assoc () on boolean

The part of the code is as follows:

//Mostrar informacion de usuario logueado
$iduser = $_SESSION['id_usuario'];
$sql ="SELECT u.Idusuario, a.NombreA FROM usuarios AS u INNER JOIN alumno AS A ON u.IdAlumno = a.IdAlumno
        WHERE u.Idusuario= '$iduser'";
    $resultado = $conexion->query($sql);
    $row = $resultado->fetch_assoc();
if (!empty($_POST)) {
    $codigo = mysqli_real_escape_string($conexion,$_POST['cod']);
    $asignatura = mysqli_real_escape_string($conexion,$_POST['nom']);
    $nota = mysqli_real_escape_string($conexion,$_POST['nota']);
    $costo = mysqli_real_escape_string($conexion,$_POST['costo']);
    $vermaterias =" SELECT idasignaturas, codigoasignaturas,
                    nombreasignatura, nota , costo
            FROM asignaturas
            WHERE codigoasignaturas = '$codigo ' AND IdAlumno = '$iduser ' ";
    $existemateria = $conexion->query($vermaterias);
     $filas = $existemateria->num_rows;
    if ($filas>0) {
        echo "
            alert('La Asignatura ya existe');

        $sqlmateria = "INSERT INTO asignaturas(
        if ($resultadomateria>0) {
            echo "
            alert('Registro Exitoso');

            echo "
            alert('Error al Registrar');

$materias = " SELECT u.Idusuario, m.idasignaturas, m.codigoasignaturas,
            m.nombreasignatura, m.nota
            FROM usuarios AS u INNER JOIN asignaturas AS m ON u.Idusuario = m.IdAlumno
            WHERE u.Idusuario = '$iduser' ";
$resultadomaterias = $conexion->query($materias);

At the moment of execution, it does not show me anything on the screen, but when I delete the line:

$row = $resultado->fetch_assoc();

This already works with errors, if someone could advise me I would appreciate it very much.


asked by Hinojosa Aguilar Oscar 26.11.2018 в 16:09

2 answers


It is possible that the SQL is not correct or has a problem that does not return the execution and the value of $ result is not a correct resource object value. For the error that is returning you $ result will be returning "false" instead of the SQL object

Before performing:

$row = $resultado->fetch_assoc();

You should make sure that the result is a valid resource pointer.

This would be a simple example:

if ($conexion->connect_errno) {
    printf("Conexión fallida: %s\n", $conexion->connect_error);

if($resultado = $conexion->query($sql)){
    $row = $resultado->fetch_assoc();
} else {
   printf("Error: %s\n", $conexion->error);

You have more information on the execution help of MySQLi fetch-assoc

answered by 26.11.2018 в 17:16

The query sql is returning false, so the error, what happens most likely is in the section of the sql where you are assigning "nicknames"

FROM usuarios AS u INNER JOIN alumno AS A

However, you are using "a" as the nickname of your tables.

answered by 26.11.2018 в 21:30