I wrote this code and I'm not sure how to get it to work. I want to get the 3 last indexes, titles and comments, all in different variables. I want index1, title1 and comment1 to get from the last row in the table, index2, title2 and comment2 to get the second from the bottom row and the index3, title3 and comment3 with the third from the bottom row. I don't really know PHP so please be patient with me :)
Thanks in advance! Itay.
Code:
<?php
$con = mysqli_connect("MYHOST", "MYUSER", "MYPASS", "MYDB");
$statement = mysqli_prepare($con, "SELECT TOP 3 * FROM table ORDER BY index DESC);
mysqli_stmt_bind_param($statement, "iss", $index, $title, $comment);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $index, $title, $comment);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["index1"] = $index;
$response["index2"] =
$response["index3"] =
$response["title1"] = $title;
$response["title2"] =
$response["title3"] =
$response["comment1"] = $comment;
$response["comment2"] =
$response["comment3"] =
}
echo json_encode($response);
?>
You could Query the length of the table you want, then just search the table but with index with it decrementing off of length.
Emil Moe
Senior Data Engineer
I'm not sure where it fails and what error and exactly what you want to achieve. But I have never seen
topbeing used, that might just be me.. But you should be able to removetop 3and addLIMIT 3at the end instead