This is a bit of a tricky one, and may seem lengthy but it's all small snippets of code and a fairly "simple" issue. Really need help with this one (and I'm NOT allowed to use PDO or any other database handler method other than this one, as defined by the school):
I have this class (abridged with relevant fields only)
class Publication {
public $num_author;
public $author;
public function __construct(..., $set_num_author) {
$this->num_author = $set_num_author;
}
public function setAuthor($set_author) {
$this->author = $set_author;
}
public function getNumAuthor() {
return $this->num_author;
}
}
And I'm trying to set its Author field. To do so, I need to use the numAuthor value for a query (Author's name and its number are in different tables), namely:
"SELECT title, abstract, body, DATE_FORMAT(date, '%d-%m-%Y'), type, updated_user_id FROM publication WHERE type = 0 and enabled = 1 ORDER BY created_at DESC";
//gets me the author id
"SELECT name FROM person WHERE id = ?";
//...which I then use to get the author's name
I instance the publications like so:
$stmt_pubs_medicos->bind_result($title, $abstract, $body, $date, $type, $num_author);
while ($stmt_pubs_medicos->fetch()) {
$pub = new model\Publication($title, $abstract, $body, $date, $type, $num_author);
array_push($latest_publications, $pub);
}
}
And I save them into latest_publications array as I expect to only get the 3 first (only showing the first, I unrolled the loop):
$stmt_author->bind_param('i', $pub->getNumAuthor());
$stmt_author->bind_result($author_name);
$stmt_author->fetch();
$latest_publications[0]->setAuthor($author_name);
But there's a problem. Once I show the publications in the webpage's div, although the body and date of each publication is different, as it should be, the author is the same. Namely the one from the 2nd latest publication ($latest_publications[1]). Why does this happen?
EDIT:
$stmt_autor = $db->conn->prepare($query_autor);
$stmt_autor->bind_param('i', $latest_publications[0]->getNumAuthor());
$stmt_autor->bind_result($nome_autor);
$stmt_autor->fetch();
$latest_publications[0]->setAuthor($nome_autor);
$stmt_autor = $db->conn->prepare($query_autor);
$stmt_autor->bind_param('i', $latest_publications[1]->getNumAuthor());
$stmt_autor->bind_result($nome_autor);
$stmt_autor->fetch();
$latest_publications[1]->setAuthor($nome_autor);
$stmt_autor = $db->conn->prepare($query_autor);
$stmt_autor->bind_param('i', $latest_publications[2]->getNumAuthor());
$stmt_autor->bind_result($nome_autor);
$stmt_autor->fetch();
$latest_publications[2]->setAuthor($nome_autor);
$pub = new model\Publication($title, $abstract, $body, $date, $type, $num_autor);-$num_autorshould be$num_authorright?$stmt_pubs_medicos->bind_result($title, $abstract, $body, $date, $type, $num_autor);var_dump()to monitor your vars until you find the bit that's causing the issue. This is most likely a design-related problem. You may also want to take a look at this to make your code easier to understand, it's hard to make some guesses, we're not the ones developing the application.