Get the latest 4 donations that come into the system .
Donation IDs are auto incremented (201 , 202, 203 , 204) .. therefore I wanted to order by DESC to get the latest 4 donations.
$resultsdonations = mysql_query("SELECT * FROM donationstable ORDER BY 'donation_id' DESC LIMIT 0,4 ");
Remove the apostrophes around 'donation_id' and the 0 is unnecessary in the LIMIT clause considering your requirement:
$resultsdonations = mysql_query(
"SELECT * FROM donationstable ORDER BY donation_id DESC LIMIT 4");
Try this:
$resultsdonations = mysql_query("SELECT * FROM donationstable ORDER BY donation_id DESC LIMIT 0,4 ");
(drop the ' around the donation_id column)
You don't need the quotes around donation_id. Just use .. BY donation_id DE ..
As the other answers have pointed out, you must remove the quotes from around donation_id.
As it currently stands, you are not sorting by the value in the donation_id column, but rather by the string literal 'donation_id', which is constant for each record and therefore results in an indeterminate ordering.
Related
I need to check if a record is either 0 or higher/equal current time. For this IN doesn't work, anyone have an idea?
This is my SQL:
$sql = "SELECT * FROM domains WHERE tld='dk' AND whoisexpire !='' AND (whoisupdate='0' OR whoisupdate>='".time()."') AND majrefd>=25 AND majtf>=10 ORDER BY whoisexpire LIMIT 25";
Updated SQL line (removed quotes around integer value)
SELECT * FROM domains WHERE tld='dk' AND whoisexpire !='' AND (whoisupdate=0 OR whoisupdate>=".time().") AND majrefd>=25 AND majtf>=10 ORDER BY whoisexpire LIMIT 25
To clarify some things up, the issue is that it keeps returning the same domains, even though a record does not match with the SQL OR whoisupdate>=".time()."
How about this?
$sql = "SELECT * FROM domains
WHERE tld='dk'
AND whoisexpire !=''
AND (whoisupdate = 0 OR whoisupdate >= NOW())
AND majrefd>=25
AND majtf>=10
ORDER BY whoisexpire
LIMIT 25";
I'm trying to return the row from my database, with the highest UID, where the URL column matches http://urltocheck.com.
I've tried all manner of things I can think of, and this is the closest I can get, but I'm getting an SQL syntax error.
My Table is called Adam, and I have the columns... UID (unique), URL (plus loads more). I'm trying to access the MySQL databse via PHP.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` ASC;
LIMIT 1;";
Can anyone help please?
You shoul use order DESC and remove the ";" after ASC
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY `UID` DESC
LIMIT 1";
Try like this. Also, remove ; at this line ORDER BY UID ASC; (didn't noticed that earlier) because of which limit 1 not coming to picture.
SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
and `UID` = (select max(`uid`) from `Adam`)
with the highest UID
You should order by UID desc and limit to 1.
You can also ORDER BY MAX ID.
<?php
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`) DESC;";
This is executed faster.
$query = "SELECT * FROM `Adam`
WHERE URL='http://urltocheck.com'
ORDER BY MAX(`UID`);";
?>
Here is my problem, so far I got this script:
$result = mysql_query(
"SELECT *
FROM $tableVideos
ORDER BY VideoTimestamp DESC
LIMIT 6"
) or die(mysql_error());
And that will make it so that I can show the 6 newest uploaded videos to my site.
But how can I have a limit of 6 and start from the 6th newest video and go up to the 12th? In that way I could show 6 videos and make a next button to show the next 6 videos.
You currently have LIMIT 6 to get the first 6 results. If you use
LIMIT a,b
then you will get b items starting from the ath.
You want
LIMIT 6,6
Meaning "Start with the 6th entry, and give me 6 more".
Read more about it.
Use this:
$result = mysql_query("SELECT * FROM $tableVideos ORDER BY VideoTimestamp DESC LIMIT 6,6") or die(mysql_error());
Actually Limit function have two paramter :
one is limit of record and second is start from where.
result = mysql_query("SELECT * FROM $tableVideos ORDER
BY VideoTimestamp DESC LIMIT 6,6") or die(mysql_error());
I want to get all rows count in my sql.
Table's first 2 columns look like that
My function looks like that
$limit=2;
$sql = "SELECT id,COUNT(*),dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
$stmt = $this->db->prepare($sql);
$stmt->execute();
$stmt->bind_result($id, $total, $datetime, $title, $content);
$stmt->store_result();
$count = $stmt->num_rows;
if ($count > 0) {
while ($stmt->fetch()) {
Inside loop, I'm getting exact value of $total, but MySQL selects only 1 row - row with id number 1. (and $count is 1 too)
Tried this sql
SELECT id,dt,title,content FROM news ORDER BY dt DESC LIMIT 2
All goes well.
Why in first case it selects only 1 row? How can I fix this issue?
for ex my table has 5 rows. I want to get 2 of them with all fields, and get all rows count (5 in this case) by one query.
Remove COUNT(*). You will only ever get 1 row if you leave it in there.
Try adding GROUP BY dt if you want to use COUNT(*) (not sure why you're using it though).
EDIT
Fine, if you insist on doing it in a single call, here:
$sql = "SELECT id,(SELECT COUNT(id) FROM news) as total,dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
This is likely cause by the variable $limit being set to 1, or not being set and mysql defaulting to 1. Try changing your first line to
$sql = "SELECT id,COUNT(*),dt,title,content FROM news ORDER BY dt DESC";
EDIT
Change to:
$sql = "SELECT SQL_CALC_FOUND_ROWS,id,dt,title,content FROM news ORDER BY dt DESC LIMIT " . $limit;
And then use a second query with
SELECT FOUND_ROWS( )
to get the number of rows that match the query
This totally wreaks of a HW problem... why else besides a professor's retarded method to add complexity to a simple problem would you not want to run two queries?
anyways.... here:
SELECT id, (SELECT COUNT(*) FROM news) AS row_count, dt, title, content FROM news ORDER BY dt DESC LIMIT
I am pretty new to MySQL...so I am sure this is probably an easy fix for someone...I've tried tutorials and other help topics, but can't figure out why it isn't working.
I have a data table with an auto incrementing index. I want to select the last 2 rows of the table. This is what I have:
SELECT * FROM tburg_golf ORDER BY 'index' DESC LIMIT 2
For some reason though, it gives me the first two rows. I've tried removing the limit, changing DESC to ASC...everything I can think of.
If curious, this is part of a larger piece of code:
$result = mysql_query("SELECT * FROM tburg_golf ORDER BY 'index' DESC LIMIT 2");
while($row = mysql_fetch_assoc($result)) {
$date = $row['date'];
$day = $row['day'];
$time = $row['time'];
$icon = $row['icon'];
$forecast = $row['forecast'];
$updated = $row['updated'];
echo $date.$day.$time.$icon.$forecast.$updated.'<p>';
}
SELECT * FROM tburg_golf ORDER BY `index` DESC LIMIT 2
If you type 'index' MySQL will ORDER BY the string 'index', which is the same for all tuples, instead of your column index.
You're ordering by 'index' which is a string constant. Column names are instead surrounded by `
Provided your auto incrementing column is actually called index, this will work;
SELECT * FROM tburg_golf ORDER BY `index` DESC LIMIT 2
Ordering by 'index' will sort every line by the same string, which basically will give you rows in random order.
Is there a field in the table called "index"?
If there is I believe the word index is a reserved word in MySQL which is why it might not work. Either rename the field or wrap it with ` instead of ' e.g.
SELECT * FROM tburg_golf ORDER BY `index` DESC LIMIT 2