I'm building a simple message board and need to get information from 3 different tables to display the main summary page. I'm using mysqli_stmt protocol and can't figure out how to get the first message and author name for each discussion, since all sql queries need to be closed before another is prepared.
Here is a simplified version of the code I run to create my database:
CREATE TABLE `users` (
`id` INT(25) UNSIGNED AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR(65) NOT NULL
);
CREATE TABLE `discussions` (
`id` INT(25) UNSIGNED AUTO_INCREMENT PRIMARY KEY ,
`subject` VARCHAR(120) NOT NULL ,
`last_author` INT(25) NOT NULL
);
CREATE TABLE `messages` (
`id` INT(25) UNSIGNED AUTO_INCREMENT PRIMARY KEY ,
`discussion` INT(25) NOT NULL ,
`author` INT(25) NOT NULL ,
`message` TEXT
);
Is it possible to form a query that will select all discussions, and for each discussion, select the subject, last author's name, and the most recent message text?
I can use this code to display all the subjects of each discussion:
<?php
$sqlcmd = "SELECT * FROM _discussions";
$get = mysqli_prepare($sqlConnection,$sqlcmd);
mysqli_stmt_execute($get);
mysqli_stmt_bind_result($get, $id, $subject, $last_author);
while(mysqli_stmt_fetch($get)) : ?>
<li><?=$subject?></li>
<?php endwhile;
mysqli_stmt_close($get);
?>
but how do I get the author name and message if I can't initiate another sql query within the loop?