Sign in
Log inSign up

how do I use Top to get multiple strings of multiple lines form SQL

Itay Meir's photo
Itay Meir
·Jun 14, 2016

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);
?>