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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:53:59+00:00 2026-06-05T22:53:59+00:00

I’m working on a small online system to sort my books, but stumbled upon

  • 0

I’m working on a small online system to sort my books, but stumbled upon a problem now I’ve been trying for hours to solve without any luck.

In my database I have a table titles with the book information (title_id, title, author…) and a second table title_relations with the fields title_relation_id, title_id, to_title_id and titlerelation.

When I call upon the information of a book there is a field related titles which is supposed to list all prequels, sequels and spin-offs.

The database looks like this:

titles

title_id  1
title     Lord of the Rings: The Fellowship of the Ring
...


title_id  2
title     Lord of the Rings: The Two Towers
...


title_id  3
title     Lord of the Rings: The Return of the King
...

title_relations

title_relation_id    1
title_id             1
to_title_id          2
titlerelation        prequel

title_relation_id    1
title_id             1
to_title_id          3
titlerelation        prequel

Now I’m calling up the information for The Fellowship of the Ring and want to be shown links to The Two Towers and The Return of the King. How do I go about getting that information there?

I got it working for one title relation, but for more than that I’d need a

foreach($title_relations as $row) {
} 

where it saves the information from title_relations into variables ($to_title_id_1, $titlerelation_1, $to_title_id_2, $titlerelation_2, …), an array or something else like that.

Nothing I tried worked, so any help offered would be highly appreciated.

I’m using PDO to get the database information.

Old code (not working):

try {
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$dbh->exec("SET CHARACTER SET utf8");

if($page == 'title'){
    #titles zuordnen 
    $titles = $dbh->prepare("SELECT * FROM titles WHERE title_id = $id");
    $titles->execute();

    while($row = $titles->fetch(PDO::FETCH_OBJ)) {
        $title = $row->title;
        /* deleted the other infos */
    }

    #title_relations zuordnen
    $title_relations = $dbh->prepare("SELECT * FROM title_relations WHERE title_id = $id");
    $title_relations->execute();

    while($row = $title_relations->fetch(PDO::FETCH_OBJ)) {
        $to_title = $row->to_title_id;
        $relation_type = $row->titlerelation;
    }

    #to_title Seriennamen zuordnen
    $series_name = $dbh->prepare("SELECT * FROM titles WHERE title_id = $to_title");
    $series_name->execute();

    while($row = $series_name->fetch(PDO::FETCH_OBJ)) {
        $series = $row->title;
    }
}

#Datenbank schließen
$dbh = null; } catch(PDOException $exceptionpdo){
echo 'ERROR: ' . $exceptionpdo->getMessage(); }

Current code (working!):

try {
    $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $dbh->exec("SET CHARACTER SET utf8");

    if($page == 'title'){
        // titles zuordnen
        // Here I binded the $id in execute, not in prepare
        $titles = $dbh->prepare("SELECT title FROM titles WHERE title_id = ?");
        $titles->bindParam(1, $id, PDO::PARAM_INT);
        $titles->execute();

        // Here you are expecting a single row, I guess title_id is a primary key, so you don't needa loop
        $row = $titles->fetch(PDO::FETCH_OBJ);
        $title = $row->title;

        // title_relations zuordnen
        $title_relations = $dbh->prepare("SELECT title_relation_id, title_id, to_title_id, titlerelation FROM title_relations WHERE title_id = ?");
        $title_relations->bindParam(1, $id, PDO::PARAM_INT);
        $title_relations->execute();

        $series = array(); // In this array we will store all the related titles

        while($row = $title_relations->fetch(PDO::FETCH_OBJ)) {
            // zu_title Serieninfo zuordnen
            $series_info = $dbh->prepare("SELECT title_id, title FROM titles WHERE title_id = ?");
            $series_info->bindParam(1, $row->to_title_id, PDO::PARAM_INT);
            $series_info->execute();

            while($row = $series_info->fetch(PDO::FETCH_OBJ)) {
                $series[] = $row;           
            }
        }

    #Datenbank schließen
    $dbh = null;
}

} catch(PDOException $exceptionpdo){
    echo 'ERROR: ' . $exceptionpdo->getMessage();
}
  • 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-05T22:54:00+00:00Added an answer on June 5, 2026 at 10:54 pm

    You shoul supply your query to better understand and adapt this answer to your specific needs.
    Without that the simplest example I can think of, basicaly you use just one query with left join to return the title even if it has no relations (good for simple smaller tables). In this example I just print the related titles to STDOUT with an identation.

    // Your get this from whatever, $_GET ... etc ...
    $title_id = 1; // exemple id1 = Lord of the Rings: The Fellowship of the Ring
    
    $sth = $dbh->prepare('select t1.title, t2.title
           from titles as t1
           left join title_relations as trel on (t1.title_id=trel.title_id)
           left join titles as t2 on (t2.title_id=trel.to_title_id)
           where t1.title_id=?');
    $sth->bindParam(1, $title_id, PDO::PARAM_INT);
    $sth->execute();
    $result = $sth->fetchAll();
    
    // original title
    echo $result[0]['title'];
    
    // related title
    foreach($results as $row)
    {
        echo "\t".$row['title'];
    }
    

    @update
    Based on your code, a working version would be:
    @disclaimer, this is based on your code, I am not recomanding to do select in a while loop, I will just use a single query like the version above (or a variation of it).

    try
    {
        $dbh = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        $dbh->exec("SET CHARACTER SET utf8");
    
        if($page == 'title')
        {
            $series = array(); // In this array we will store all the related titles
    
            // titles zuordnen
            // Here I binded the $id in execute, not in prepare
            $titles = $dbh->prepare("SELECT title_id, title, author /*put your other columns*/ FROM titles WHERE title_id = ?");
            $titles->bindParam(1, $id, PDO::PARAM_INT);
            $titles->execute();
    
            // Here you are expecting a single row, I guess title_id is a primary key, so you don't needa loop
            $row = $titles->fetch(PDO::FETCH_OBJ);
            $title = $row->title;
            /* deleted the other infos */
    
            // title_relations zuordnen
            $title_relations = $dbh->prepare("SELECT title_relation_id, title_id, to_title_id, titlerelation FROM title_relations WHERE title_id = ?");
            $title_relations->bindParam(1, $id, PDO::PARAM_INT);
            $title_relations->execute();
    
            while($row = $title_relations->fetch(PDO::FETCH_OBJ))
            {
                // This is in a loop, it will store just the value from the last row !
                // $relation_type = $row->titlerelation;
    
                // to_title Seriennamen zuordnen
                $series_name = $dbh->prepare("SELECT title_id, title, author /*put your other columns*/ FROM titles WHERE title_id = ?");
                $series_name->bindParam(1, $row->to_title_id, PDO::PARAM_INT);
                $series_name->execute();    
    
                while($row = $series_name->fetch(PDO::FETCH_OBJ))
                {
                    // $series = will store the values just from the last row, changed it to an array to store all relations
                    $series[$row->title_id] = $row->title;
                }
            }
        }
    
        // Datenbank schließen
        $dbh = null;
    } catch(PDOException $exceptionpdo)
    {
        echo 'ERROR: ' . $exceptionpdo->getMessage();
    }
    

    Your code return just one relation because you query-ed just the last row returned from title_relations. To solve this (again in the context of your code) I moved the last query in the while of title_relations.
    To be able to acces all relations I made an (array)$series, you just store the last row in the (string)$series.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.