Insert Rows at the top of the table

Asked

Viewed 60 times

2

I have the following problem, I created a form in which the value that the user has to write goes to the top of the table, leaving the oldest down, or make my SELECT can select only the last 10 records of the table. Can someone help me?

UPDATING!!!

My code is like this :

$result = "SELECT id, filmes FROM dados_rec ORDER by filmes DESC LIMIT 10";
$query = mysqli_query($link, $result);

$row = mysqli_fetch_row($result);

echo $row[1];
echo $row[2];
echo $row[3];

It prints only the first, which in case is the name of a movie, but it ignores the other "Excerpts"

  • I think it’s simpler to make a select with ORDER BY campo DESC

  • I’ve already done it, but it shows me in alphabetical order, I just need it to show me the latest records

  • If you have a field with the registration (date and time) of the business order by it or you can do by id.

  • Can you explain me how to do by id? I started now in php and know almost nothing.

  • Instead of placing the field with title/name in the ODER BY puts the id field.

2 answers

1


To catch the last 10:

SELECT * FROM `table` ORDER BY id DESC LIMIT 10

To display all lines:

$sql = "SELECT id,nome FROM `table` ORDER BY id DESC LIMIT 10";
$result = mysql_query($sql, $connection) or die (mysql_error());
$row = mysql_fetch_array($result);


while($row = mysql_fetch_array($result))
{ 
    echo $row['id']." - ."row['nome'];
}
  • It worked, thank you, but when I ask to print the lines of the table it only prints the first line.I’m using "echo $Row[1];" the first line printa certinho, but the others do not, has how to help me?

  • @Luvinicius how are you printing? ask the question, so I can help you.

  • I edited it, see if you can help me, it seems a simple mistake, but I can’t fix it at all.

0

No need to do this, just select the last records from the table using

SELECT * FROM dados_rec ORDER BY id DESC
  • I’ve already done it, but it shows me in alphabetical order, I just need it to show me the latest records.

  • you must be selecting the record movies, I will edit the answer and see if it works

Browser other questions tagged

You are not signed in. Login or sign up in order to post.