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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:23:08+00:00 2026-06-14T14:23:08+00:00

I currently have 5 tables in MySQL database. Some of them share foreign keys

  • 0

I currently have 5 tables in MySQL database. Some of them share foreign keys and are interdependent of each other. I have tested the query in MySQL and it works perfect. When using the query php it is displaying an error with database PDO connection. Also, I am not sure if my PHP will print the results like I am intending. How can I resolve these two issues?

PHP

<?

try {

    $pdo = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
      $query = $pdo->prepare("SELECT course.id, major.name, course.name, course.code, course.description, course.hours, semester.semester, semester.year
                            FROM course
                            LEFT JOIN major_course_xref ON course.id = major_course_xref.course_id
                            LEFT JOIN major ON major.id = major_course_xref.major_id
                            LEFT JOIN course_semester_xref ON course.id = course_semester_xref.course_id
                            LEFT JOIN semester ON course_semester_xref.semester_id = semester.id");
      $query->execute();

      foreach ($pdo->query($query) as $row) {
        print $row['id'] . "\t";
        print $row['name'] . "\t";
        print $row['name'] . "\n";
        print $row['code'] . "\n";
        print $row['description'] . "\n";
        print $row['hours'] . "\n";
        print $row['semester'] . "\n";
        print $row['year'] . "\n";
    }

      unset($pdo); 
      unset($query);

?>  

Desired Display:

Computer Engeneering
Visual Studio I
CPE1900
Introduction to disciplined, object-oriented programming
4
Fall
2013

Update after Rays answer: This the error that I get now

Warning: PDO::query() expects parameter 1 to be string

Warning: Invalid argument supplied for foreach() in 
  • 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-14T14:23:10+00:00Added an answer on June 14, 2026 at 2:23 pm

    The execute() method returns FALSE on error, or TRUE if successful. Then you need to fetch the rows one at a time.

    See: http://php.net/manual/en/pdostatement.execute.php and http://www.php.net/manual/en/pdostatement.fetch.php

    if ($query->execute()){
    
          while ($row = $query->fetch(PDO::FETCH_ASSOC)){
            print $row['id'] . "\t";
            print $row['name'] . "\t";
            print $row['name'] . "\n";
            print $row['code'] . "\n";
            print $row['description'] . "\n";
            print $row['hours'] . "\n";
            print $row['semester'] . "\n";
            print $row['year'] . "\n";
        }
    }
    else
        echo 'Could not fetch results.';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have 5 tables in MySQL database. Some of them share foreign keys
I currently have 5 tables in MySQL database. Some of them share foreign keys
I currently have 5 tables in MySQL database. Some of them share foreign keys
Greetings, I have some mysql tables that are currently using an md5 hash as
I'm trying to normalize a mysql database.... I currently have a table that contains
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
Suppose I am building an app using current mysql database tables. I have this
I need some advice of how to setup my tables I currently have a
Scenario: I load some data into a local MySQL database each day, about 2
Here's some background info. I have three MySQL tables (all InnoDB). The first table

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.