Reputation: 152
I have a php function which has two query from same table. the first query select some rows and the second query must update the selected rows after selection in one column. but unfortunately only the first query is execute and I can't update the table.
this is my function code:
public function displayNewMessage(){
include "connection.php";
// first query
$displayMsg = $connection->prepare("select date, senderUserName, message from messages where showed = 0 and
((senderUserName = ? and receiverUserName = ?) or (senderUserName = ? and receiverUserName = ?))
order by date desc");
// second query
$checkMsg = $connection->prepare("UPDATE messages SET showed = 1 where showed = 0 and
((senderUserName = ? and receiverUserName = ?) or (senderUserName = ? and receiverUserName = ?))");
// preparation for both query is same
$displayMsg->bind_param("ssss", $senderUserName, $receiverUserName, $receiverUserName, $senderUserName);
$senderUserName=$this->getSenderUserName();
$receiverUserName=$this->getReceiverUserName();
$displayMsg->execute(); // execute first query
$checkMsg->execute(); // execute second query
$displayMsg->store_result();
$displayMsg->bind_result($messageDate, $senderUserName, $message);
while($displayMsg->fetch()){
$this->setSenderUserName($senderUserName);
$this->setMessage($message);
$this->setMessageDate($messageDate);
?>
<span style="color:#00F"> <?php echo $this->getSenderUserName(); ?></span> says: <br />
<?php echo $this->getMessage(); ?>
<div class="date"><?php echo $this->getMessageDate(); ?></div><br />
<?php
}
$displayMsg->free_result();
}
and if I execute the second query at end of the function it would run first and cause the condition of first query to evaluate to false, so it wouldn't select any row.
Upvotes: 0
Views: 89
Reputation: 1642
As per your question but unfortunately only the first query is execute and I can't update the table..
According to mysqli documentation you MUST:
Note: When using mysqli_stmt_execute(), the mysqli_stmt_fetch() function must be used to fetch the data prior to performing any additional queries.
You are calling the second execute before the fetch loop; this is not allowed.
As for: and if I execute the second query at end of the function it would run first and cause the condition of first query to evaluate to false, so it wouldn't select any row.
I am not sure what you mean by "it will run first".
Upvotes: 1