Jump to content

Using a while loop, how can I display the last five entries into the database?


jdock1

Recommended Posts

I am using a while loop to display the database entries. Now how could I make it display the LAST five entries into the database?

 

Ive been coding all day and I have writers block or something! I cant think.

 

Also, how could I make PHP count how many entries there are in the database? For example, I want to put up on my page how many signatures have been added to the database. How could I do this? I have been searching around everywhere and still couldnt find a solution.

Link to comment
Share on other sites

Look into the MySQL "LIMIT" clause. E.g.

 

select * from your_table limit 0, 5

 

That selects only the first 5 rows matched. In order to return the last five, you will need to "ORDER BY" a chronological field; either an auto incrementing number, or a date field. E.g.

 

select * from your_table order by entry_id limit 0, 5

select * from your_table order by entry_date limit 0, 5

Link to comment
Share on other sites

Look into the MySQL "LIMIT" clause. E.g.

 

select * from your_table limit 0, 5

 

That selects only the first 5 rows matched. In order to return the last five, you will need to "ORDER BY" a chronological field; either an auto incrementing number, or a date field. E.g.

 

select * from your_table order by entry_id limit 0, 5

select * from your_table order by entry_date limit 0, 5

Thanks!

Now how could I track how many entries there are in the database and echo it?

Link to comment
Share on other sites

That would be mysql_num_rows

 

<?php
$result = mysql_query("select * from your_table order by entry_date"); //you wouldn't want to limit this query.
$rows = mysql_num_rows($result);
echo $rows;
?>

 

Good luck!

Thanks, but for some reason I am getting an access denied error (using password NO) even know I established a connection to the database properly..? Its saying the error is on line 4, where the $result variable is being defined.

 

Why would it be giving me that error?

 

My code:

 

<?php
$db = mysqli_connect('localhost', 'adsco_ptn', 'password', 'adsco_petitions')
or die('Error: Could not connect to database!');
$result = mysql_query("select * from signatures order by first"); //you wouldn't want to limit this query.
$rows = mysql_num_rows($result);
echo $rows;

 

Error:

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'adsco'@'localhost' (using password: NO) in /home/adsco/public_html/google/count.php on line 4

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/adsco/public_html/google/count.php on line 4

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/adsco/public_html/google/count.php on line 5

 

Link to comment
Share on other sites

That would be mysql_num_rows

 

<?php
$result = mysql_query("select * from your_table order by entry_date"); //you wouldn't want to limit this query.
$rows = mysql_num_rows($result);
echo $rows;
?>

 

Good luck!

 

"you wouldn't want to limit this query".. why exactly?

Link to comment
Share on other sites

It shouldn't be limited because he asked how to "track how many entries are in the database."

 

Which also brings up that mysql_num_rows() isn't the best way to do that. A SELECT COUNT() query  more efficient when simply returning the number of records in a table.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.