Ok, I am using PEAR DB (this is the first project, I'm trying to decide if I like it or not)
I am making a discussion board and I want to post the name of the topic and the number of posts in parenthesis
like
New Libraries (5)
My question is how to work with two queries at once... here is my function:
////////////////////////////////////////////////////////////////////////////////////
function displayTopics($section_key, $p_root_key = 0) {
global $ttDB;
// display title
$stmt = "SELECT s_title FROM section where s_key=$section_key";
$section_title = $ttDB->getOne($stmt);
print "<h2 align=\"center\">IT Discussion Base</h2>\n";
print "<h3 align=\"center\">$section_title</h3>\n";
// display topics in this section
// Get all posts with root of 0, format date, join with user table, to get the name
$stmt = "SELECT p_key, p_root_key, p_title, p_body, u_name, DATE_FORMAT(p_cwhen, '%m/%d/%y %r') as p_cwhen FROM post, users WHERE p_root_key=$p_root_key AND p_section_key=$section_key AND u_key=p_cwho ORDER BY p_cwhen asc ";
if (DB::isError($ttResult = $ttDB->query($stmt))) {
echo $ttResult->getDebugInfo();
}
print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" align=\"center\" width=\"600\">\n";
$i = 0; // counter to determine odd/even rows for color classes
while ($row = $ttResult->fetchRow()) {
extract($row); // extract array to variables
// get count of posts for the current p_key
$stmt2 = "SELECT count(*) FROM post WHERE p_root_id=$p_key";
$num_posts = $ttDB->getOne($stmt2);
$class = "class=\"" . ((($i % 2) == 0) ? "evenRow" : "oddRow") . "\"";
print "<tr $class><td><a href=\"display.php?nextPage=showDetails&s_key=$section_key&p_key=$p_key\">$p_title ($num_posts)</a></td><td>$u_name</td><td align=\"right\" class=\"timedate\">$p_cwhen</td></tr>\n";
$i++;
}
print "</table>\n";
print "<br><br><br>";
print "<div align=\"center\"><a href=\"display.php?nextPage=showAddTopic&s_key=$section_key&p_root_key=0&type=Topic\">Add Topic</a></div>\n";
} // end of displayTopics
I use the getOne method to get the count of that topic (p_key) where its key is the root_id ... and the error I get is:
test topic 1 () Nola 10/29/01 11:23:50 AM
test topic 2 () Nola 10/29/01 12:54:47 PM
Warning: extract() expects first argument to be an array in /export/devel/nstowe/it/display.php on line 172
So, there seems to be a problem using the two queries, because it .... works without the getOne query.
Any ideas?
Thanks 🙂