Trying to get property of non-object in line 18

2

In the line that there is error is in this:

if ($resultado->num_rows() > 0) ... 

This is my code:

<?php
    $mysqli= new mysqli('localhost','id5840508_root','******','id5840508_hospital
');

    $salida="";
    $query="SELECT*FROM paciente ORDER By id";

    if (isset($_POST['consulta'])) {
        $q=$mysqli->real_escape_string($_POST['consulta']);
        $query="SELECT * FROM paciente WHERE codigo_pac LIKE '%".$q."%' OR  apellido_paterno LIKE '%".$q."%' OR  apellido_materno LIKE '%".$q."%' 
                    OR  nombre LIKE '%".$q."%'" ;

    }

    $resultado= $mysqli->query($query);



            if ($resultado->num_rows() > 0) {
        $salida.=" <table class='tabla_datospac'>
                <thead>
                    <tr>
                        <td>Nº</td>
                        <td>  CÓDIGO </td>
                        <td>APELLIDO PATERNO</td>
                        <td>APELLIDO MATERNO</td>
                        <td>NOMBRE</td>
                        <td>FECHA DE NACIMIENTO</td>
                        <td>SEXO</td>
                        <td>ZONA</td>
                        <td>CALLE</td>
                        <td>Nº DOM.</td>
                        <td>LOCALIDAD</td>
                        <td>SALARIO MENSUAL</td>
                        <td>OCUPACION ACTUAL</td>
                        <td>FECHA / INGRESO</td>



                    </tr>
                </thead>
                <tbody>";   

                while ($fila= $resultado->fetch_assoc()) {
                $salida.="  <tr>
                                <td >".$fila['id']."</td>
                                <td>".$fila['codigo_pac']."</td>
                                <td>".$fila['apellido_paterno']."</td>
                                <td>".$fila['apellido_materno']."</td>
                                <td>".$fila['nombre']."</td>
                                <td>".$fila['fecha_nac']."</td>
                                <td>".$fila['sexo']."</td>
                                <td>".$fila['zona']."</td>
                                <td>".$fila['calle']."</td>
                                <td>".$fila['nro']."</td>
                                <td>".$fila['localidad']."</td>
                                <td>".$fila['salario_mensual']."</td>
                                <td>".$fila['ocupacion_actual']."</td>
                                <td>".$fila['fecha_ingreso_a_trabajo']."</td>



                            </tr>";
                }

                $salida.="</tbody></table>";

                } 
                else
                {
                $salida.="<H4>NO EXISTE</h4>";
                }

                echo $salida;
                $mysqli->close();
}

?>
    
asked by KVN20 20.05.2018 в 21:30
source

2 answers

1

Try the code like this. It's what I call a controlled flow code .

There may be an error in your second query.

Here, whatever happens, the code will tell you exactly what error is happening, since nothing is taken for granted: all variables are evaluated .

<?php
$mysqli= new mysqli('localhost','id5840508_root','******','id5840508_hospital
');

if ($mysqli){
    $query="SELECT * FROM paciente ORDER By id";

    if (isset($_POST['consulta'])) {
        $q=$mysqli->real_escape_string($_POST['consulta']);

        /*
           *OJO: Esta consulta es vulnerable a ataques de Inyección SQL
           *cuando en una consulta intervienen datos externos
           *como la variable $q aquí utilizada
           *la práctica recomendada es el uso de consultas preparadas
        */
        $query="SELECT * FROM paciente WHERE codigo_pac LIKE '%".$q."%' OR  apellido_paterno LIKE '%".$q."%' OR  apellido_materno LIKE '%".$q."%'
                    OR  nombre LIKE '%".$q."%'" ;

    }
    $resultado= $mysqli->query($query);

    if($resultado){

        if ($resultado->num_rows > 0) {

            /*
                *OJO AQUÍ: para ser coherentes, declaramos $salida
                *como una VARIABLE NUEVA en esta 1ª línea de esta parte
                *De hecho, $salida se declara como tal en cada parte del flujo
            */

            $salida=" <table class='tabla_datospac'>
                <thead>
                    <tr>
                        <td>Nº</td>
                        <td>  CÓDIGO </td>
                        <td>APELLIDO PATERNO</td>
                        <td>APELLIDO MATERNO</td>
                        <td>NOMBRE</td>
                        <td>FECHA DE NACIMIENTO</td>
                        <td>SEXO</td>
                        <td>ZONA</td>
                        <td>CALLE</td>
                        <td>Nº DOM.</td>
                        <td>LOCALIDAD</td>
                        <td>SALARIO MENSUAL</td>
                        <td>OCUPACION ACTUAL</td>
                        <td>FECHA / INGRESO</td>
                    </tr>
                </thead>
                <tbody>";

            while ($fila= $resultado->fetch_assoc()) {
                $salida.="  <tr>
                                <td >".$fila['id']."</td>
                                <td>".$fila['codigo_pac']."</td>
                                <td>".$fila['apellido_paterno']."</td>
                                <td>".$fila['apellido_materno']."</td>
                                <td>".$fila['nombre']."</td>
                                <td>".$fila['fecha_nac']."</td>
                                <td>".$fila['sexo']."</td>
                                <td>".$fila['zona']."</td>
                                <td>".$fila['calle']."</td>
                                <td>".$fila['nro']."</td>
                                <td>".$fila['localidad']."</td>
                                <td>".$fila['salario_mensual']."</td>
                                <td>".$fila['ocupacion_actual']."</td>
                                <td>".$fila['fecha_ingreso_a_trabajo']."</td>
                            </tr>";
            }

            $salida.="</tbody></table>";
        }
        else
        {
            $salida.="<H4>NO EXISTE</h4>";
        }
    }
    else
    {
        $salida="Error en la consulta: ".$mysqli->error;
    }

    $mysqli->close();

}
else
{
    $salida="Error de conexión a la base de datos";

}
echo $salida;

?>
    
answered by 20.05.2018 / 22:01
source
1

It's a paradigm error, you're working with the mysqli driver and trying to use object-oriented style; therefore the next line

if ($resultado->num_rows() > 0) {

It should be as follows

if ($fila = $resultado->num_rows > 0) {

For further reference I leave you the official documentation for you to corroborate

I also tell you that you have the wrong line

$query="SELECT*FROM paciente ORDER By id";

This misspelled should be like this

$query="SELECT * FROM paciente ORDER BY id";
  

Do not write the query all together or you will not know how to interpret it

    
answered by 20.05.2018 в 21:39