Data does not appear on page

3

I made an OO code in PHP. I used MySQLi for the database and WAMPSERVER as the server, where I inserted data into the "category" table. The bank collation is "latin2_general_ci". There were no errors, but in the browser the data entered did not appear. Below is the code:

<?php
$conecta = new mysqli("localhost", "root", "", "teste_forum");
if($conecta->connect_error){
die("Connection failed: " . $conecta->connect_error);
}


class categoria{

    private $id;
    private $titulo;

    /*public function conexao($conecta){
        $conecta = new mysqli("localhost", "root", "", "teste_forum");
    }*/

    public function setId($id){
        $this->id  = $id;
        }

    public function getId(){
        return $this->id;
        }

    public function setTitulo($titulo){
        $this->titulo = $titulo;
        }

    public function getTitulo(){
        return $this->titulo;
        }

    function listar(categoria $categoria){
        try{
            global $conecta;
            $query = $conecta->query("SELECT titulo FROM categoria");
            return $query;
            /*$rs = $query->fetch_assoc();
            $categoria->setTitulo($rs["titulo"]);*/
        }
        catch(Exception $e){
            echo "ERRO!";
        }
    }

    function inserir(categoria $categoria){
        try{
            global $conecta;
            $query = $conecta->prepare("INSERT INTO categoria (titulo)    VALUES (?)") or die ($conecta->error);
            $query->bind_param("s", getTitulo());
            $query->execute();
            if ($query->affected_rows > 0){
                return true;    
            }
        }
        catch(Exception $e){
            echo "ERRO!";
        }
    }

}

?>

   <html>
   <head>
   <meta charset="utf-8">
   <style type="text/css">

     h3{
      color: red;
     }

     h4{
       color: steelblue;
     }

     form{
       border: 1px #333 solid;
     }

    .nome{
       color: green;
     }

    .coment{
      color: violet;
     }

 </style>
 </head>
 <body>
 <?php


     $categoria = new Categoria;
     $cat_listar = $categoria->listar($categoria);

     while($reg = $cat_listar->fetch_array()){
          echo "<h3>......... " . $categoria->getTitulo($reg["titulo"]) . ".........</h3>";
     }

?>

<br><br>
<a href="form.php">Ir ao formul�rio cadastrar categorias</a>

In the figure below, I would like the browser to show the titles.

Andtheotherfigurebelowshowstheresult.Thetitleshadtobeinthereddots.

All I want is for the titles to be displayed.

    
asked by anonymous 21.05.2015 / 20:37

2 answers

1

fetch_array () returns an array if the category object has no validation or formatting just do echo with $reg is the simplest form.

echo '<h3>'. $reg[0] .'</h3>';

//também é possível imprirmir o nome através de índice não númerico

echo '<h3>'. $reg['titulo'] .'</h3>';

getTitulo() does not receive parameter it seems that you confused with setTitulo() , in the code below no assignment of values was made at all.

 $categoria = new Categoria;
 $cat_listar = $categoria->listar($categoria);

 while($reg = $cat_listar->fetch_array()){
      echo "<h3>" . $categoria->getTitulo($reg["titulo"]) . "</h3>";
 }

For method listar() return a category array (object) make the following modification

public function listar(){
   global $conecta;

   $query = $conecta->query($query);
   $arr = array();

   while($item = $query->fetch_array()){
      $c = new categoria();
      $c->setTitulo($item[0]);
      $arr[] = $c;
   }

   return $arr;
}
    
21.05.2015 / 20:45
0

I increased the insert function ():

 function inserir(categoria $categoria){
        try{
            global $conecta;
            $query = $conecta->prepare("INSERT INTO categoria (titulo) VALUES (?)") or die ($conecta->error);
            $query->bind_param("s", setTitulo());
            $query->execute();
            if ($query->affected_rows > 0){
                return true;    
            }
        }
        catch(Exception $e){
            echo "ERRO!";
        }
    }

So I could use the loopback below:

    while($reg = $cat_listar->fetch_array()){
    echo "<h3>......... " . $reg["titulo"] . ".........</h3>";
}

And it worked:

    
21.05.2015 / 21:17