PHP repeating database data

Asked

Viewed 78 times

-2

Hello. I’m making a website for student purposes, taking into account that the connection to the database is perfect, when it comes to getting the data stored in the database, PHP duplicates the content on the page. As in the following image (Ignore the image, because its url is the same for all):

[![s][1]][1]

<?php 

            $server ='localhost';
            $user ='root';
            $password ='root';
            $dbname ='site_salgados';
            $port ='3306';

            $db_connect = new mysqli($server, $user, $password, $dbname, $port);
            mysqli_set_charset($db_connect, "utf8");

            if ($db_connect->connect_error == true) {
            echo 'Falha ao se conectar! erro: ' . $db_connect->connect_error;

            } else {
            $sql = "SELECT DISTINCT nome, descricao FROM produtos";
                $result = $db_connect->query($sql);

            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) { 

                $nome = $row['nome']; 
                $descricao = $row['descricao']; ?>

                     <div class="container mt-5 mb-4 ">
                            <div class="row">
                                <div class="col-md-6">
                                    <div>
                                    <a href="https://www.google.com"><img src="gallery/1.jpg"></a>
                                    <h6><a href="#"><?php echo $nome; ?></a></h6>
                                    <p><?php echo $descricao; ?></p>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div>
                                    <a href="https://www.google.com"><img src="gallery/1.jpg"></a>
                                    <h6><a href="#"><?php echo $nome; ?></a></h6>
                                    <p><?php echo $descricao; ?></p>
                                    </div>
                                </div>
                            </div>
                    </div>

                <?php }
            }
            }

        ?>

Could someone help me? I’ve been trying to solve this mistake for some time.

  • I’m using the CSS framework, Boostrap 4.

1 answer

1


Hello, so from what I see in your code you’re echo twice the same variable

<?php 

                $server ='localhost';
                $user ='root';
                $password ='root';
                $dbname ='site_salgados';
                $port ='3306';

                $db_connect = new mysqli($server, $user, $password, $dbname, $port);
                mysqli_set_charset($db_connect, "utf8");

                if ($db_connect->connect_error == true) {
                echo 'Falha ao se conectar! erro: ' . $db_connect->connect_error;

                } else {
                $sql = "SELECT DISTINCT nome, descricao FROM produtos";
                    $result = $db_connect->query($sql);

                if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) { 

                    $nome = $row['nome']; 
                    $descricao = $row['descricao']; ?>

                         <div class="container mt-5 mb-4 ">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div>
                                        <a href="https://www.google.com"><img src="gallery/1.jpg"></a>
                                        <h6><a href="#"><?php echo $nome;?></a></h6>
                                        <p><?php echo $descricao; ?></p>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div>
                                        <a href="https://www.google.com"><img src="gallery/1.jpg"></a>
                                        <h6><a href="#"><?php echo $mesmo_nome_que_la_em_cima; ?></a></h6>
                                        <p><?php echo $mesma_descricao_que_la_em_cima; ?></p>
                                        </div>
                                    </div>
                                </div>
                        </div>

                    <?php }
                }
                }

            ?>

You can try the following, but generally recommend separating your layout from your controller

if ($result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) 
                    { 
                     ?>
                      <div class="container mt-5 mb-4 ">
                          <div class="row">
                               <div class="col-md-6">
                                        <div>
                                           <a href="https://www.google.com"><img src="gallery/1.jpg"></a>
                                           <h6><a href="#"><?php echo $row["nome"];?></a></h6>
                                           <p><?php echo $row["descricao"]; ?></p>
                                        </div>
                                </div>
                      <?php 
                           $row = $result->fetch_assoc();
                      ?>
                          <div class="col-md-6">
                                       <div>
                                         <a href="https://www.google.com"><img src="gallery/1.jpg"></a>
                                         <h6><a href="#"><?php echo $row["nome"];?></a></h6>
                                         <p><?php echo $row["descricao"]; ?></p>
                                         </div>
                           </div>
                     </div>
                  </div>
                <?php }
        }
      }

  ?>
  • Thank you very much! Solved! Thank you for dedicating your time helping me :D

  • I consider myself a Junior Programmer in PHP for knowing the basics and a little more... If it’s not too much to ask, you could be more specific with "Separate Controller Layout"?

  • Simply put in files the layout i.e., what the person sees , in others the logical part what the person does not see, and instead of calling all the code within html, creates a php function and calls only that function when necessary, but this is if you want to continue only with php and html without resorting to frameworks, cleaner and still advanced, is to use HTML , CSS for the layout , Javascript for the interaction, dynamism and send the requests in AJAX to your controller , ie your PHP, inform you about the MVC architecture

  • Thanks for the tips! I had another problem doing it the way you told me. When adding one more element to the database, a div with H2 and a paragraph is created on the page, expecting me to create two more contents in the database. How could I get PHP to just create new content on the page with only what’s in the bank? A hug and thanks for the help! (I’ll edit the post and post a print of what I mean)

  • 1

    Do not edit the post, ask another question

  • Okay. I hope you can answer me before the toxic public on this site makes anyone I’m not allowed to ask new questions with.

  • 1

    @Joãovictor If you keep making edits to the question after accepting the system itself will block you to ask new questions. Your comment was unfair, nor do the moderators have the prolongation to prevent questions from being asked. Moderators and members of the community have the prerogative to close the question by perceiving flaws or inconsistencies in the formulation of the question, but this does not mean that the question is definitively closed. If you look in the frame just below the question is described the reason for the closure if you edit the question it goes to a row of analysis...

  • ...queue that I just found this question and then the question will be reviewed and if it is clarified regarding the reason for closing it will be reopened. Read: My question was closed, I can’t get an answer there?. Having doubts about how to ask [Ask]. Questions about the operation of the site, do our [tour].

Show 3 more comments

Browser other questions tagged

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