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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:35:39+00:00 2026-06-05T04:35:39+00:00

In the db, there are projects. Each projects has files. Each file has multiple

  • 0

In the db, there are projects. Each projects has files. Each file has multiple versions.

So theoretically, I could do this (pseudo);

$res = mysql_query("SELECT * FROM projects");
while($row=mysql_fetch_assoc($res))
{
    echo "Project ".$row["id"]."<br/>";
    $res2 = mysql_query("SELECT * FROM projectfiles");
    while($row2=mysql_fetch_assoc($res2))
    {
        echo "FileID ".$row2["id"]."<br/>";
        $res3 = mysql_query("SELECT * FROM fileversions");
        while($row3=mysql_fetch_assoc($res3))
        {
            echo $row3["name"]."<br/>";
        }
        echo "<br/>";
    }
}

Sample output:

Project 1
    FileID 1
        test_file_1.txt.1

    FileID 2
        test_file_2.txt.1

    FileID 3
        test_file_3.txt.1
        test_file_3.txt.2

But this would mean load and loads of mysql queries where only one would be needed.

So I join the queries:

$sql = "SELECT projectfiles.*, fileversions.*, 
    projects.id as projects_id
   FROM projects";

$sql .= " LEFT JOIN".
    " (SELECT 
        projectfiles.id as projectfiles_id,
        projectfiles.fileID as projectfiles_fileID,
        projectfiles.projectID as projectfiles_projectID
       FROM projectfiles
       ) AS projectfiles".
    " ON projects.id = projectfiles_projectID";


$sql .= " LEFT JOIN".
    " (SELECT 
        fileversions.id as fileversions_id,
        fileversions.name as fileversions_name,
        fileversions.location as fileversions_location,
        fileversions.fileID as fileversions_fileID
       FROM fileversions
       ) AS fileversions".
    " ON projectfiles.projectfiles_fileID = fileversions_fileID";

But this now, of course, leaves me with unstructured data:

enter image description here

So what I did was:

while($row = mysql_fetch_assoc($res))
{
    $projectID = $row["projects_id"];
    $fileID = $row["projectfiles_fileID"];
    $fileversionID = $row["fileversions_id"];

    $fileversionsArray[$fileversionID] = array($row["fileversions_name"],$row["fileversions_location"]);
    $fileArray[$fileID][$fileversionID] = $fileversionID;
    $projectArray[$projectID][$fileID] = $fileID;
}

So I can show it like:

foreach($projectArray as $projectID => $projectDatas)
{
    echo "Project ID: ".$projectID."\n";
    foreach($projectDatas as $fileID)
    {
        echo "\tFile ID: ".$fileID."\n";
        foreach($fileArray[$fileID] as $fileversionID)
        {
            echo "\t\tFile version name: ";
            echo $fileversionsArray[$fileversionID][0];
            echo "\n";
            echo "\t\tFile location: ";
            echo $fileversionsArray[$fileversionID][2];
            echo "\n";
        }
    }
}

Which gives the output:

ex2

But I’m not so sure if I’m even gaining any performance doing this, because there’s alot of duplicate data in the joined rows, and it sure is alot of work to update the code once/if stuff in the db changes.

I guess in short; this just feels like a dirty workaround to a proper solution which I believe is out there.

Is there a better solution to this?

  • 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-06-05T04:35:41+00:00Added an answer on June 5, 2026 at 4:35 am

    There is no way to return structured data from MySQL database directly. Your effort to convert these table oriented data to arrays is correct.

    Take a look at PHP dibi library, the ->fetchAssoc() method (doc) which does everything you need in nice and short syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have multiple Python projects that have dependencies on each other. Hierarchically, these are
There are two projects: 1) applet project that outputs jar file 2) web app
I have a folder structure that contains multiple javascript files, each of these files
I just opened my visual studio 2005 solution file with multiple projects and all
I have a file that has over 200 lines in this format: name old_id
Say I have the following solution with multiple versions of the same code each
I'm working on a Flash project that deals with multiple files. There is a
I'm having a tricky MSBuild issue. There is a solution file containing multiple web
In various projects there are certain parts I will keep jumping to. Is there
When building ASP.NET projects there is a certain amount of boilerplate, or plumbing that

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.