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 6927037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:59:46+00:00 2026-05-27T10:59:46+00:00

I have got a table in my MySql database: pageid | text ———–+———- 1

  • 0

I have got a table in my MySql database:

  pageid   | text
-----------+----------
    1      | test
-----------+----------
    2      | example
-----------+----------
    3      | another
-----------+----------
    1      | example1

and this PHP code:

$query = "SELECT pageid FROM test";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
  {
  $id = $row['pageid'];
  $id1 = array($id);
  $id2 = array_unique($id1);
  foreach ($id2 as $value)
    {
    echo $value . "<br>";
    }
  }

But it returns:

1
2
3
1

I would like to make the pageids unique. So I would like to echo out the number 1, once. Like this:

1
2
3

So what am I doing wrong? How could I achieve what I want?

Thanks in advance…

[I know that this code doesn’t make much sense, but this is just a demo for this question and I have a much more complicated code 🙂 ]

  • 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-27T10:59:47+00:00Added an answer on May 27, 2026 at 10:59 am

    Of course, the real answer in this situation is to select distinct records to begin with:

    $query = "SELECT DISTINCT pageid FROM test";
    

    But as to why your code doesn’t work…

    You are declaring $id1 as an array, but not appending to it. Instead you’re just creating a 1 element array each time. Instead, append all results then call array_unique()

    $query = "SELECT pageid FROM test";
    $result = mysql_query($query);
    
    // Declare array first
    $id1 = array();
    while($row = mysql_fetch_array($result))
      {
      $id = $row['pageid'];
      // Append to the array...
      $id1[] = $id;
      }
    
    // Call array_unique() outside the loop:
    $id2 = array_unique($id1);
    foreach ($id2 as $value)
    {
      echo $value . "<br>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database with two tables (simplified for this question): movies table
I have a small PHP mysql query looking at a table in my database.
I got an error while creating a table in php with mysql database, and
I have got a simple MySQL table and primary index (id) is not numbered
I've got a mysql database with a table (InnoDB) of Games: gamerooms id: bigint(20)
I have a mySQL database table with a column that corresponds to an image
I have two set of arrays in different MYSQL table. This what I want
I have a mysql database with a table named games and a blob field
I've got a PHP MySQL query to grab featured products from a MySQL database,
I have a Zend/PHP script that reads rows from a table in one MySQL

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.