Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:54:51+00:00 2026-05-26T22:54:51+00:00

This query outputs each result 6 times: So, for one row with a body

  • 0

This query outputs each result 6 times: So, for one row with a body of ‘ayo’, it will be outputted as: ayo
ayo
ayo
ayo
ayo
ayo

function get_comments($page_type, $spot) {
        $query = mysql_query("
                              SELECT
                              comments.user_id AS user_id,
                              comments.page_type AS page_type,
                              comments.spot AS spot,
                              comments.comment AS comment,
                              comments.timestamp AS timestamp,
                              users.first_name AS first_name,
                              users.last_name AS last_name
                              FROM comments, users
                              WHERE page_type = '$page_type' AND spot = '$spot' AND (comments.user_id = users.user_id OR comments.user_id = '0')
                              ORDER BY timestamp DESC
                             ");

        while($fetch = mysql_fetch_assoc($query)) {

            $comment = $fetch['comment'];
            $timestamp = $fetch['timestamp'];
            $timestamp_date = date('M d y\'', $timestamp);
            $timestamp_time = date('g:m', $timestamp);
            $comments_user_id = $fetch['user_id'];

                if(date('H', $timestamp) > 12) {
                    $am_pm = 'pm';
                    } else {
                        $am_pm = 'am';
                        }

            $first_name = $fetch['first_name'];
            $last_name = $fetch['last_name'];

            ?>

            <div id="replies">

                <?php if($comments_user_id == '0') {echo 'Guest';} else { echo $first_name.' '.$last_name; } ?><br />
                <?php echo $timestamp_date; ?><br />
                <?php echo $comment; ?>

            </div>

            <?php

            }
        }

In here $page_type and $spot refer to: http://localhost/$page_type/$spot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:54:52+00:00Added an answer on May 26, 2026 at 10:54 pm

    You’re getting duplicates because your implicit join condition contains this:

    OR comments.user_id = '0'
    

    and you have multiple rows with comments.user_id = 0. Each of these user_id = 0 rows will appear match your join condition and that will lead to duplicates. The solution is to fix your data so that each comment has a valid user_id where “valid” means “has an entry in users” and the drop the comments.user_id = 0 part of your join condition. And while you’re at it, switch to InnoDB tables and add foreign keys to enforce this referential integrity constraint.

    BTW, if user_id is an integer then you really shouldn’t be quoting it; you’re making the database do unnecessary work and building a bad habit that will cause you some pain and suffering if you every use another database. You should also switch to an explicit join:

    from comments join users on comments.user_id = users.id
    where page_type = '$page_type' and spot = '$spot'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to query this DB to get each row, but also the SUM
I have this working query that outputs URL and click counts from database. <?php
This query is somewhat related to this earlier one on sorting where, it was
I have managed to bring the following output with this query SELECT DISTINCT IF(purchaseproduct_customerid=0,contactperson,vendorname)
I'm logging output with log = /var/log/mysql/query.log and this works ok, but I only
This Query is giving me an error of #1054 - Unknown column 'totalamount' in
this query SELECT * FROM tblContracts LEFT JOIN tblPartys ON tblContracts.id = tblPartys.Contract_id INNER
This query works fine for me: $query = SELECT p.topnode_id, p.param_key, p.param_value FROM tbl_params
This query gives me syntax error in when-between line. how can i solve them?
This query selects all the unique visitor sessions in a certain date range: select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.