Use of undefined constant length - assumed 'length'

1

I have the following php code, I miss the title error

public function traer_testis($id) {

        $sql = 'SELECT * FROM testimonios WHERE id_testimonio ="'.$id.'"';

        $rs = $this->consulta($sql);

        $datos = array();
        //$registros = "";
        while ($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {
            $registros = new stdClass();
            $registros->id_testimonio = $row["id_testimonio"];
            $registros->nombre_testimonio = $row["nombre_testimonio"];
            $registros->descripcion_testimonio = $row["descripcion_testimonio"];
            $registros->descripciond_testimonio = $row["descripciond_testimonio"];
            $registros->lugar_testimonio = $row["lugar_testimonio"];
            $registros->color_testimonio = $row["color_testimonio"];
            $registros->puesto_testimonio = $row["puesto_testimonio"];
            $registros->img_testimonio = $row["img_testimonio"];
            $datos[] = $registros;


        }
        if ($datos.length == 0) {
            $registros = '<tr>';
            $registros .= '<td colspan="5">';
            $registros .= 'No existen registros ...';
            $registros .= '</td>';
            $registros .= '</tr>';
        }

        //$arr = array('registros' => $registros);
        //return ($arr);
        return  ($datos);
    }

then when I return the object I get another error

  

PHP Notice: Array to string conversion in C: \ wamp64 \ www \ teo360 \ dashmin \ classes \ adminteo.class.php on line 57

this is the other code

    <?php 
        require_once '../clases/adminteo.class.php';

        $id = $_POST['seleccionado'];

        $adminteo = new adminteos();
        $datos = $adminteo->traer_testis($id);
        echo json_encode($datos);

?>
    
asked by Cesar 02.02.2018 в 15:22
source

1 answer

2

It's because array in php does not contain a length property. Use the count() method:

if (count($datos) == 0) {
    $registros = '<tr>';
    $registros .= '<td colspan="5">';
    $registros .= 'No existen registros ...';
    $registros .= '</td>';
    $registros .= '</tr>';
 }
    
answered by 02.02.2018 в 15:34