Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP PDO connection and sql query
    text
    copied!<p>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? </p> <p><strong>PHP</strong></p> <pre><code>&lt;? try { $pdo = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw"); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e-&gt;getMessage() . "\n"; exit; } $query = $pdo-&gt;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-&gt;execute(); foreach ($pdo-&gt;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); ?&gt; </code></pre> <p><strong>Desired Display:</strong></p> <pre><code>Computer Engeneering Visual Studio I CPE1900 Introduction to disciplined, object-oriented programming 4 Fall 2013 </code></pre> <p><strong>Update after Rays answer:</strong> This the error that I get now</p> <pre><code>Warning: PDO::query() expects parameter 1 to be string Warning: Invalid argument supplied for foreach() in </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload