PHP PDO displaying table

I’m new to PDO having used mySQL in the past. Currently I have managed to get the table from the database printed out and displaying one course at a time. I need to print all of the courses.
Here is my current code:

[code]<?php
require_once(‘connect.php’);
$sql = “SELECT CourseID, Course_Name FROM coursename WHERE CourseID = :CourseID”;
$stmt = $db->prepare($sql);
$CourseID = 1;
$stmt->bindParam(’:CourseID’, $CourseID, PDO::PARAM_INT);
$stmt->execute();

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

print_r($results);

?> [/code]

This outputs “Array([0] => Array ( [CourseID] => 1 [Course_Name] => Computing ) )” I have tried to implement a while loop with no success.
Any help would be appreciated.

Your question is not clear on what you want to do. I think if you download my PDO bumpstart database in my signature that may help you. It has several basic features that are often used.

I have edited my post to hopefully make my question clearer. I will have a look at the PDO bumpstart database now.

Thanks that did help but I’m currently getting the error “Notice: Undefined index: CourseID in N:\ftp\comp \comparison.php on line 46”. Line 6 in code below.

[code]<?php
require_once(‘connect.php’);
$sql = “SELECT * FROM coursename WHERE CourseID=?”;
$stmt = $db->prepare($sql);
$stmt->execute(array(
$_POST[‘CourseID’]
));

$result = $stmt->fetchAll();

?>

Courses

<th colspan="3"></th>
<?php foreach ($result as $row) { echo <<<EOT
        </tr>

EOT;

    } // End Foreach

?>


CourseID Course_Name
{$row['CourseID']} {$row['Course_Name']} View Details
[/code] CourseID is a column in my database btw.

I believe this is what you have to do and this is untested.
[php] <?php
require_once(‘connect.php’);
$sql = “SELECT CourseID, Course_Name FROM coursename WHERE CourseID = :CourseID ORDER BY CourseID ASC”;
$stmt = $db->prepare($sql);
$CourseID = 1;
$stmt->bindParam(’:CourseID’, $CourseID, PDO::PARAM_INT);
$stmt->execute();
while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
echo “

” . $output->Course_Name . “

\n”;
}
[/php]

I just tried what you suggested strider64 and it displayed one course name.
Could you suggest how to display all the course names and courseID’s?
Edit
I added CourseID to the echo line and that printed it out. Im trying to print out more than one course now and im guessing a chage of line 3 and/or 5 is needed .

Its currently working using the below code.

[code]<?php
require_once(‘connect.php’);
$sql = "SELECT CourseID, Course_Name FROM coursename ORDER BY CourseID ASC ";
$stmt = $db->prepare($sql);

$stmt->bindParam(':CourseID', $CourseID, PDO::PARAM_INT);
$stmt->execute();
while ($output = $stmt->fetch(PDO::FETCH_OBJ)) {
   echo "<p>" . $output->Course_Name  . " " . $output->CourseID  . "</p>\n";

}
?> [/code]
Thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service