Why is the PHP MySQL result showing up twice?

Having an issue with my PHP script that is fetching results from the localhost WAMP server. When I print out the query, each record is being displayed twice!

Check out the screenshot below for a visual representation:

This is the PHP MySQL script:

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$movie_lists = "";
$no_result = "";

$servername = "localhost";
$username = "root";
$password = "localpass";
$dbname = "movieadventuredb";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM movielisting ORDER BY ID DESC LIMIT 10";
$result = $conn->query($sql);

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

    $id = $row["id"];
    $movie_quality = $row["movie_quality"];
    $release_date = strftime("%Y", strtotime($row["movie_release_date"]));

    $movie_lists .= "<div class='item item-1'><a href='#'><img src='inventory_images/$id.jpg'/></a>
                        <a href='#'><div class='overlay'>
                            <p class='movie_quality $movie_quality'>$movie_quality</p>
                            <p class='movie_year year'>$release_date</p>
                        </div></a>
                    </div>";
     }
   } else {
      $movie_lists = "";
   }
      $conn->close();
 ?>

The CSS code for the listing can be found below:

.recent_movies_slider .recent_movie_lists {float:left; width:97%;}
.slider-horizontal {width:90%; margin:25px auto; *background:#eee; height:240px; z-index:1;}
.slide...

<p>Here is the HTML code to display the result:</p>

<pre><code><div class="recent_movie_lists">
   <div id="slider" class="slider-horizontal">
       <?php echo $movie_lists; ?>
   </div>
</div>

Answer №1

Consider using this code snippet: $movie_lists = (all your code); in place of $movie_lists .= (all your code).

Apologies for the confusion earlier, as already mentioned in the comments. Have you thought about directly echoing the while loop within your div?

<div class="recent_movie_lists">
   <div id="slider" class="slider-horizontal">
       <?php
       if ($result->num_rows > 0) {
// output data of each row
   while($row = $result->fetch_assoc()) {
       //echo "id: " . $row["id"]. " - Name: " . $row["movie_name"]. " " .     $row["movie_quality"]. "<br>";

    $id = $row["id"];
    $movie_quality = $row["movie_quality"];
    $release_date = strftime("%Y", strtotime($row["movie_release_date"]));

    echo "<div class='item item-1'><a href='#'><img src='inventory_images/$id.jpg'/></a>
                        <a href='#'><div class='overlay'>
                            <p class='movie_quality $movie_quality'>$movie_quality</p>
                            <p class='movie_year year'>$release_date</p>
                        </div></a>
                    </div>";


     }
   } else {
      echo "";
   }
?>
   </div>
</div>

Assuming that you are combining HTML and PHP in the same file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for submitting my Angular form?

I am facing a challenge with submitting updated information to the database from my edit page. Despite its apparent simplicity, I have been unable to find a suitable example that aligns with my specific situation. This final stage of my project has left me ...

"The submission of multiple forms does not properly update the MySQL database

I've encountered an issue with my function and first query working correctly but the second involves multiple form inputs like id[], urunad, birim... I'm using $_POST to pass data to the function. $id2 = $gid; $sid2 = $sid; $urunad2 = $urunad; ...

Combine all select commands in a relational table in MySQL into one JSON column

I have a table with headers that include the id and name of individuals. Additionally, there are multiple tables containing specific details about each individual, such as phone number, address, gender, birthplace, etc. How can I construct a SELECT comman ...

js issue with passing form data to use with PHP mail function

As a novice, I am diving into the world of HTML webpage creation. Utilizing a free online template, my current project involves developing a Contact Page that triggers a php script to send an email with the captured fields. While I've successfully man ...

Looking for a CMS that focuses on the front end? (Or maybe a CMF

Looking for a CMS system without a front end package? I need a full administration suite that can output content to XML so we can create the front end from scratch. The client wants an open source solution, any recommendations or plugins in Drupal or Jooml ...

Problematic PHP/AJAX: How come my getAttribute function is only selecting the initial line within the foreach loop?

My goal here is to display a list of users, each with a button that sends unique IDs to the database. The issue I'm facing is that the code only works for the first user in the list and not the rest. I suspect this is because every list item has the s ...

Issue with WHERE clause in mysql not functioning

When I have a row labeled Brand New in the column labeled status, running a query like the one below does not return anything: SELECT * FROM table WHERE status LIKE '%new%' However, when I run a query like this, it does retrieve that specific r ...

Utilizing CodeIgniter with PHPMailer for streamlined email delivery using Google SMTP servers

Seeking assistance with transitioning this form to G SMTP from PHP. My attempts thus far have been unsuccessful; can anyone pinpoint the issue? Appreciate any help! Here is my existing script: if (count($email_body) > 0) { ...

Issues with datepicker functionality in forms

I have tested multiple datepicker plugins, such as the jQuery UI datepicker and others, but none of them seem to work with my current code. I am in need of a datepicker that can be added to this form of mine. Any suggestions or help would be greatly appr ...

Ways to improve the speed of MySQL INSERT queries

I am currently running a program that collects information from multiple merchants. For each request received from a merchant, my program executes an INSERT query: INSERT INTO `good` (id, code, merchant, netcost, ip) VALUES ('', 'GC8958&ap ...

.htaccess causing issues with Ajax when rewriting URLs

After setting up the basic project and configuring htaccess for URL rewriting, I also implemented jQuery AJAX. Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f Rewri ...

PHP - Unusual behavior of basic preg_replace function

Consider the following array: '@(.*?)<div>(.*?)</div>(.*?)@i' => 'ok' Using preg_replace, I am attempting to retrieve the value "ok" from a given string. However, there seems to be an issue where the last (.*?) is not ...

Unable to access Heroku login using cURL and PHP

A straightforward PHP script I created for logging into a specific webpage runs smoothly on my localhost using Wamp server. However, when I attempt to run it on Heroku, I encounter a response like this: [image link](https://i.stack.imgur.com/uilks.png). ...

Embedding services into Entities: Verifying the presence of an image file

Currently, I am working with PHP and the Doctrine ORM. The situation at hand involves a product table that has an image field. My goal is to create an event that verifies whether an image exists in my assets cloud storage (specifically Amazon S3). If the f ...

Show a dynamic dropdown box using AJAX if a specific variable is present

I am currently working on implementing an ajax call for a dynamic dropdown box. Once the variable $place is available, it triggers an ajax call to populate the dropdown box. My goal is to pass the variable $place to listplace.php, encode it as JSON data ...

What is the best way to update a dropdown field in a form without having to refresh the entire form by utilizing ajax?

Within a form, there is a dropdown field populated with item names from a database. If a new item needs to be added, the user can click on the "Add New" button to open a pop-up window and save the new item. After saving the new item in the database, it d ...

Execute a webpage in the background using PHP

Currently, I'm working on creating a basic PHP script to fetch four images from a server. The catch is that each time I retrieve a new image, I have to access a webpage first - like this: http://example.com/Set.cgi?Image=1 I don't have the abili ...

Dealing with Errors and Exceptions in PHP7

Just made the switch to php7 and encountered this error message: Error: argument 1 passed to MyClass\Throwable::exceptionHandler() must be an instance of Exception, instance of Error given Here is the relevant code snippet from the class: namespace ...

What could be the reason my foreach loop is not being run?

After creating a login form that successfully grants access upon entering the correct email and password, there seems to be an issue. A foreach loop is implemented to test all results, with the assumption of only one account existing. foreach ($result as ...

Pressing the "Enter" key will submit the contents of

Hello, I have recently created a new chat box and everything seems to be working fine. However, I am facing an issue with submitting a message when I press enter (to go to the function Kucaj()). Can anyone provide assistance with this problem? I tried ad ...