I have a website , and I created a book library in it.
The table is: book
It contain: book_id
, book_title
, book_cat
, book_img
, book_content
, active
, book_date
, visits
, book_sender
)
The 'book_sender' is the ID of the sender because any logged in user can add books , I wanted to make a gift each week to the best sender (who sent alot of books) so I wanted to show top 3 sender's of the week in a PHP file to make challenge between the top3.
Champ
----------
book_id
book_title
book_cat
book_img
book_content
active
book_date
visits
book_sender
the sql query goes like this:
select book_sender, count(book_id) as num_books
from champ
where DATEDIFF(sysdate(), book_date) <= 7
group by book_sender
limit 0, 3;
Code snippet (replace db_name, db_user, and db_password with appropriate values):
<?php
$db = new PDO('mysql:host=localhost;dbname=db_name;charset=UTF8', 'db_user', 'db_password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stm = $db->prepare( "
SELECT book_sender `Sender ID`, COUNT(*) `Books Sent`
FROM Champ
WHERE book_date > DATE_ADD( now(), INTERVAL -7 DAY )
GROUP BY book_sender
LIMIT 0, 3" );
$stm->execute();
while( $row = $stm->fetch(PDO::FETCH_ASSOC) ) {
$result[] = $row;
}
// Step through $result and print out
?>
Also assuming you're using MySQL and PDO.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.