Data does not appear on the page

Asked

Viewed 105 times

3

I made an OO code in PHP. I used Mysqli for the database and WAMPSERVER as the server, in which I entered data for the table "category". The collation of the bank is "latin2_general_ci". No errors occurred, but in the browser the registered data 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 highlight that I wanted the browser to show the titles.

Figura 1 - Dados da tabela categoria

And the other figure below shows the result. The titles had to be between the little red dots.

Figura 2 - Resultado...não esperado

All I want is for the titles to be shown.

2 answers

1

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

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 seems that you have confused with setTitulo(), in the code below at no time was the values assigned.

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

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

For the method listar() return a category(object) array 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;
}

0

I incremented 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 repeat loop below:

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

And it worked:

Resultado esperado

  • How did it work? on this line $query->bind_param("s", setTitulo()); setTitulo() does not return anything, it is to insert a title to the property $object title.

  • It’s because I just wanted SELECT to work, but thanks so much for the warning.

Browser other questions tagged

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