Warning illegal string offset

Asked

Viewed 47 times

-2

I created a class with arrays to be populated with objects, selected the database, then made a foreach to create a new object with each row of the select return and save these objects in the array.

But when I try to create a new object I get the following error message:

Warning illegal string offset in ['cd_anuncio'] checked in the bank and is equal.

I used the print_r to check if there was a return, and I checked that the return is correct.

public static function getAnuncios() {

  include("conexao.php");
  $contador = 0;

  $query = "SELECT * FROM tb_anuncio";

  $result = $conexao - > query($query);

  $formatResult = mysqli_fetch_assoc($result);
  print_r($formatResult);

  if (!isset($anuncios)) {

    foreach($formatResult as $anuncio) {
        $obj_anuncio = new anuncio($anuncio['cd_anuncio'],
        $anuncio['nm_titulo'],
        $anuncio['ds_anuncio'],
        $anuncio['cd_usuario'],
        $anuncio['nm_estado'],
        $anuncio['nm_cidade'],
        $anuncio['nm_bairro'],
        $anuncio['nm_categoria']);

        $anuncios[$contador] = $obj_anuncio - > getCd();
        $anuncios[$contador] = $obj_anuncio - > getNmTitulo();
        $anuncios[$contador] = $obj_anuncio - > getDsAnuncio();
        $anuncios[$contador] = $obj_anuncio - > getCdUsuario();
        $anuncios[$contador] = $obj_anuncio - > getNmEstado();
        $anuncios[$contador] = $obj_anuncio - > getNmCidade();
        $anuncios[$contador] = $obj_anuncio - > getBairro();
        $anuncios[$contador] = $obj_anuncio - > getCategoria();

        $contador++;#
        code...
    }
    die();
  }

}

1 answer

1


The mysqli_fetch_assoc does not return all values from the bank, it only returns one row at a time, when used foreach the value of $anuncio were actually strings, because the foreach was only a row based on the names of the columns, the correct use mysqli_fetch_assoc would be something like:

        $result = $conexao->query($query);

        if(!isset($anuncios))
        {
            while ($anuncio = mysqli_fetch_assoc($result)) 
            {
                $obj_anuncio = new anuncio($anuncio['cd_anuncio'],$anuncio['nm_titulo'],$anuncio['ds_anuncio'],$anuncio['cd_usuario'],$anuncio['nm_estado'],$anuncio['nm_cidade'],$anuncio['nm_bairro'],$anuncio['nm_categoria']);

                $anuncios[$contador]=$obj_anuncio->getCd();
                $anuncios[$contador]=$obj_anuncio->getNmTitulo();
                $anuncios[$contador]=$obj_anuncio->getDsAnuncio();
                $anuncios[$contador]=$obj_anuncio->getCdUsuario();
                $anuncios[$contador]=$obj_anuncio->getNmEstado();
                $anuncios[$contador]=$obj_anuncio->getNmCidade();
                $anuncios[$contador]=$obj_anuncio->getBairro();
                $anuncios[$contador]=$obj_anuncio->getCategoria();

                $contador++;
                # code...
            }
            die();
        }

As described in the PHP documentation: http://php.net/manual/en/mysqli-result.fetch-assoc.php#example-1936

  • Vlw by the answer, implemented and ran without error, only I have no idea how to check if the arrey was really populated you have any suggestions ?

  • @Reignomo as so?

  • @Reignomo after the while, before the die do this: if (empty($anuncios)) { echo 'Vazio'; } else { echo 'Populado'; }

  • OK vlw saved me :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.