SQL Sub Query
Hi,
I have a database which contains student names (among other things). The students can interface with the database to submit service requests. Now for the problem: Students are submitting multiple requests. We need to be able to easily locate these students. Essentially we need to accomplish the following:
1. Query the database and display all student's names currently in the DB.
2. Count the number of records which contain the same student name.
I have written the following code in an attempt to handle the task, but it does not seem quite right. Here is the code:
PHP Code:
//Step 1: Grab all names from the database!
include("connection.inc.php");
$sql = "SELECT ticketid, name FROM tickets limit 30";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
// iterate over result set
// print each row
// keeps getting the next row until there are no more to get
while ($row = pg_fetch_array($result)) {
$ticketid = $row['ticketid'];
$student_name = $row['name'];
//Now display the output
echo $student_name;
echo "<br />";
include ("connection.inc.php");
$numtickets = pg_query($dbh, "select * from tickets where name='$student_name'");
$numtickets = pg_num_rows($numtickets);
//END THE WHILE STATEMENT
}
echo "<br />";
echo "Number of students with multiple entries: " .$numtickets;
Any help would be greatly appreciated!
Thanks,
smti
smti, August 31st, 2009 11:40 PM
Bookmarks