1
I am making a user list and the data does not appear on the page. I waited for the user’s name, his type and the number of posts and responses he sent to the system to appear. There is another problem. Three users are registered, but only one appears. Below is the code:
<?php
$conecta = new MySQLi("localhost","root","","forum-ti-1");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial- scale=1">
<title></title>
<link rel="stylesheet" href="css/style.css">
<script src="js/script.js"></script>
</head>
<body>
<?php
include('templates/header.html.php');
?>
<div class="head_topics" style="top: 330px; left: 210px;">
<h2>Usuários</h2>
</div>
<table class="t_subtopics" style=" top: 370px; left:210px;">
<?php
$usuario = "SELECT usuario.nome, situacao_usuario.descricao, COUNT(discussao.titulo) As posts, COUNT(resposta.conteudo) AS respostas
FROM usuario, situacao_usuario, discussao, resposta
WHERE usuario.id_situacao = situacao_usuario.id AND usuario.id = discussao.id_usuario AND usuario.id = resposta.id_usuario";
$lista = $conecta->query($usuario);
while($rs = $lista->fetch_array()){
?>
<tr>
<td style="width: 200px;">
<div class="discussion_photo">
<img src="images/profile-photo.jpg" style="height: 80px; width: 100px; display: block;"/>
</div>
</td>
<td style="width: 500px;">
<ul>
<li><h3 style="font-size: 20px;"><? echo $rs['usuario.nome']; ?></h3></li>
<li id="discussion_type_user" style="width: 80px; padding: 5px;"><? echo $rs['situacao_usuario.descricao']; ?></li>
</ul>
</td>
<td style="width: 200px;">
<ul>
<li><? echo $rs['posts']; ?> tópicos</li>
<li><? echo $rs['respostas']; ?> respostas</li>
</ul>
</td>
</tr>
<?php
}
?>
</table>
<?php
include('templates/footer.html.php');
?>
</body>
The figure below shows the result:
I used the consultation:
"SELECT usuario.nome, situacao_usuario.descricao, COUNT(discussao.titulo) As posts, COUNT(resposta.conteudo) AS respostas
FROM usuario, situacao_usuario, discussao, resposta
WHERE usuario.id_situacao = situacao_usuario.id
AND usuario.id = discussao.id_usuario
AND usuario.id = resposta.id_usuario
GROUP BY usuario.id"
but did not return any data:
Editing
The server query worked, but did not appear on the page:
(Editing)
mysqli_error()
returns something?– Guilherme Nascimento
No, it does not return.
– Luana Santos
if you run sql directly in the database, returns something?
– Euler01
"If you run sql directly in the database, does it return anything? - Euler01 " No, I’ll fix that.
– Luana Santos
Nothing keeps popping up.
– Luana Santos
Try it like this
$conecta->query($usuario); var_dump($conecta->error());
, note that theerror
has to be used after query and not before.– Guilherme Nascimento
directly in the database works sql?
– Euler01
"directly in the database works sql? - Euler01 3 mins ago" No.
– Luana Santos
" Try $connect->query($user); var_dump($connects->error());, note that the error has to be used after the query and not before. - Guilherme Nascimento 5 minutes ago " .
– Luana Santos
If you are not returning a record running directly in the database, the problem is in SQL. Check if there is record in all tables used in sql. If the user’s id_status exists in the table of situacao_usuario, if the user has record in the discussion.
– Euler01
I changed the SQL of my answer, try running it in the database and tell me if you returned anything.
– Euler01
Fatal error: Call to a Member Function fetch_array() on a non-object The problem is with fetch_array()...
– Luana Santos
Query is wrong in this part "LEFT JOIN reply ON user.id = answer.id_user GROUP BY user"'
– Luana Santos
Yes, of course. had a comma after the excerpt "ON user.id = discussao.id_usuario" removes it.
– Euler01
Luana, if my answer solved your problem, mark it as correct, Otherwise, tell them it didn’t work. I noticed that you have several questions with answers, but you did not accept any, please accept the answers that solved your problems, the community please.
– Guilherme Nascimento