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.
And the other figure below shows the result. The titles had to be between the little red dots.
All I want is for the titles to be shown.
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.– Ricardo
It’s because I just wanted SELECT to work, but thanks so much for the warning.
– Luana Santos