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

  • Home
  • SEARCH
  • 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 6562199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:40:51+00:00 2026-05-25T13:40:51+00:00

This is essentially very simple I’ve just been rather verbose about it. My data

  • 0

This is essentially very simple I’ve just been rather verbose about it. My data is structured as shown at the bottom of my post. My goal is to organise the data in a simple spreadsheet format, ‘flattening’ the relational database. Something like:

Product1 URL
Product2 URL URL URL
Product3 URL

My initial approach is as below. The reason I’m looking for an alternative is I have > 10,000 rows in the products table and each product has an average of 5 URLs. This generates an enourmous amount of MySQL queries (in fact exceeding my shared hosting resources), there must be a better way, right? My code has been simplified to distill the essence of my question.

My Approach

//Query to get all products
$products = mysql_query("SELECT * FROM products");

$i = 0;
//Iterate through all products
while($product  = mysql_fetch_array($products)){

    $prods[$i]['name'] = $product['name'];
    $prods[$i]['id'] = $product['id'];

    //Query to get all URLs associated with this particular product
    $getUrls = mysql_query("SELECT * FROM urls WHERE product_id =".$product['id']);

    $n = 0;

    while($url = mysql_fetch_array($getUrls)){

        $prods[$i]['urls'][$n] = $url['url'];  
    }
}

The Result

To be clear, this is the intended result, the output was expected. I just need a more efficient way of getting from the raw database tables to this multidimensional array. I suspect there’s more efficient ways of querying the database. Also, I understand that this array uses a lot of memory when scaled up to the required size. As my aim is to use this array to export the data to .csv I would consider using XML (or something) as an intermediary if it is more efficient. So feel free to suggest a different approach entirely.

//The result from   print_r($prods);

Array
(
    [0] => Array
        (
            [name] => Swarovski Bracelet
            [id] => 1
            [urls] => Array
                (
                    [0] => http://foo-example-url.com/index.php?id=7327

                )
        )
    [1] => Array
        (
           [name] => The Stranger - Albert Camus
           [id] => 2
           [urls] => Array
               (
                   [0] => http://bar-example-url.com/index.php?id=9827
                   [1] => http://foo-foo-example-url.com/index.php?id=2317
                   [2] => http://baz-example-url.com/index.php?id=5644
                )
        )
)

My Database Schema

Products

id  product_name

1   Swarovski Bracelet
2   The Stranger - Albert Camus
3   Finnegans Wake - James Joyce

URLs

id  product_id  url                                                price

1   1           http://foo-example-url.com/index.php?id=7327       £16.95
2   2           http://bar-example-url.com/index.php?id=9827       £7.95
3   2           http://foo-foo-example-url.com/index.php?id=2317   £7.99
4   2           http://baz-example-url.com/index.php?id=5644       £6.00
5   3           http://bar-baz-example-url.com/index.php?id=9534   £11.50

URLs.product_id links to products.id

If you’ve read this, thank you for your incredible patience! I look forward to reading your response. So how can I do this properly?

  • 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-25T13:40:52+00:00Added an answer on May 25, 2026 at 1:40 pm

    The SQL

     SELECT p.id, p.product_name, u.url 
     FROM products p, urls u 
     WHERE p.id = u.product_id ORDER BY p.id
    

    PHP code to “flatten” the urls, I have just seperated the urls with a space.

    $i = 0;
    $curr_id;
    while($product  = mysql_fetch_array($products))
    {
        $prods[$i]['name'] = $product['product_name'];
        $prods[$i]['id'] = $product['id'];
    
        if($product['id'] == $curr_id)
        {
            $prods[$i]['urls'] .= " ".$url['url'];  
        }
        else
        {
            $prods[$i]['urls'] = $url['url']; 
        }
        $i++;
        $curr_id = $product['id'];
    }
    

    The Result

    [0] => Array
            (
                [name] => Swarovski Bracelet
                [id] => 1
                [urls] => http://foo-example-url.com/index.php?id=7327
            )
      [1] => Array
            (
               [name] => The Stranger - Albert Camus
               [id] => 2
               [urls] => http://bar-example-url.com/index.php?id=9827 http://foo-foo-example-url.com/index.php?id=2317 http://baz-example-url.com/index.php?id=5644
    
            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a very simple database (mysql) with essentially two types of data,
I have been struggling with something which in theory should be very simple for
Ok this is more of general technology/approach question. We have a very simple web
This is a rather simple example and probably wouldn't make much of a difference
I'm working on a very simple game (essentially an ice sliding puzzle), for now
I've built a custom page.php template. Very simple, essentially: <?php get_header(); ?> <?php get_sidebar();
Ill try to keep this simple and to the point. Essentially I have a
So, I essentially have what I want already, very simple, but there are some
I have a very simple JavaScript operation which is just not working. I'm using
I have a simple fiddle set up that attempts to illustrate this, but essentially

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.