i want to combine a SELECT and UPDATE query, to avoid duplicat select of rows.
Here is my example code:
private function getNewRCode() {
$getrcodesql = "SELECT * FROM `{$this->mysqlprefix}codes` WHERE `used` = 0 LIMIT 1;";
$getrcodequery = $this->mysqlconn->query($getrcodesql);
if(@$getrcodequery->num_rows > 0){
$rcode = $getrcodequery->fetch_array();
$updatercodesql = "UPDATE `{$this->mysqlprefix}codes` SET `used` = '1' WHERE `id` = {$rcode['id']};";
$this->mysqlconn->query($updatercodesql);
$updateusersql = "UPDATE `{$this->mysqlprefix}users` SET `used_codes` = `used_codes`+1, `last_code` = '{$rcode['code']}', `last_code_date` = NOW() WHERE `uid` = {$this->uid};";
$this->mysqlconn->query($updateusersql);
$output = array('code' => $rcode['code'],
'time' => time() + 60*60*$this->houroffset,
'now' => time()
);
return $output;
}
}
I would like to execute $getrcodesql
and $updatercodesql
at once, to avoid that the same code is used for different users.
I hope you understand my problem and know a solution for this.
Greetings, Frederick