I have been Trying to check if selected row is last in MYSQL data records, if it is last then go back to first record. Here is what I have done so far:
$fps = @fopen('number.txt', "r+");
$somay = fread($fps, filesize('number.txt'));
fclose($fps);
$num = ($somay+1);
$file = "number.txt";
$fh = fopen($file,'w+') or die("cant open file");
fwrite($fh,$num);
fclose($fh);
$sql = "SELECT @last_id := MAX(id),token FROM `token` where `id` = '".$somay."'";
if (!$result = $mysqli->query($sql)) {
echo "Sorry, the website is experiencing problems.";
echo "Error: Our query failed to execute and here is why: \n";
echo "Query: " . $sql . "\n";
echo "Errno: " . $mysqli->errno . "\n";
echo "Error: " . $mysqli->error . "\n";
exit;
}
if ($result->num_rows === 0) {
echo "We could not find a match for ID, sorry about that. Please try again.";
exit;
}
while ($tokennek = $result->fetch_assoc()) {
$token = $tokennek[token];
$tokenid = $tokennek[last_id];
}
if($somay == $tokenid){
$file = "number.txt";
$fh = fopen($file,'w+') or die("cant open file");
fwrite($fh,1);
fclose($fh);
}
Let me make it more clear here what I am doing is saving a file named number.txt to keep record of token which is already been used and go to next token/row.