Where do I put DESC in a mysql query? (PHP) - php

This is my query:
$query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage";
I'm trying to sort the results by the latest (latest on top), so I tried to add DESC.
But where ever I put it in the query it gives me an error, the only way it doesn't give an error is like this:
$query = "SELECT * FROM movies ORDER BY 'timestamp' DESC LIMIT $startAt, $perPage";
But even though it doesn't give an error, it still doesn't work.
This probably seems like a silly thing to ask but it's really driving me crazy

Timestamp is the datatype in MySQL. So, always try to avoid such column name in your table. Even, if you have a column named as timestamp, you must have to use backtick. So, use below query:
$query = "SELECT * FROM table ORDER BY `timestamp` LIMIT $startAt, $perPage";

Try backtick on timestamp :
$query = "SELECT * FROM movies ORDER BY `timestamp` DESC LIMIT $startAt, $perPage";
Using backticks permits you to use alternative characters
I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.

Do not use quote in field name. Try this:
$query = "SELECT * FROM movies ORDER BY timestamp DESC LIMIT $startAt, $perPage";

Related

how to use the function time in query myql

i have this query :
SELECT *
FROM news
WHERE STATE LIKE 'SI'
AND data<'".time()."'
ORDER
BY data DESC
limit 0,1
and i would like to know if the function time it's correct because there is an error on synthase.
thank's you !
There are reserved words in MySQL which you cannot use as column names without clearly indicating they are names. See:
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Both 'data' and 'date' are reserved words. Use back ticks to indicate they are used as names:
$query = "SELECT *
FROM `news`
WHERE `STATE` LIKE 'SI'
AND `data` < '".time()."'
ORDER
BY `data` DESC
LIMIT 0,1
or better, in my opinion, use better column names:
$query = "SELECT *
FROM newsItems
WHERE itemState LIKE 'SI'
AND creationDate < '".time()."'
ORDER
BY creationDate DESC
LIMIT 0,1";
As you can see I had to guess what the columns really stand for, because it's not directly clear from the names. It should be, because that's what they are there for.
TIME() is a function in which you also need to pass your parameter.
So, instead of using it blank like:
select TIME();
you need to use it in this way:
select TIME(now());
Note: In your query, you need to pass like (only if you have datetime field in your table):
AND time(data) < time(now())

php Error: Resourse id #4

for($i = 0;$i<10;$i++)
{
$query1 ="SELECT `id` FROM radcheck ORDER BY `id` DESC LIMIT 1;";
$lololo= mysql_query($query1) or die(mysql_error());
//echo $lololo;
$query = "INSERT INTO radcheck (username,attribute,op,value) VALUES ('teleuser".$lololo."','Cleartext-Password',':=','$arr1[$i]')";
mysql_query($query) or die(mysql_error());
}
I am trying to retrieve my latest id value to append with my username. And the value retrieve always be 'Resource id #4'.
Is there anyway to solve it ?
Thank you.
Mysql doesn't support TOP that is for SQL Server.
Instead of using TOP you can use mysql LIMIT so your query would be:
SELECT `id` FROM radcheck ORDER BY `id` DESC LIMIT 1;
You do not have to use single quotes around column names. if you need to escape it use backticks. And TOP is not mysql syntax. You have to use limit
SELECT `id` FROM radcheck ORDER BY `id` DESC limit 1
Do not longer use the depricated mysql_* API. Use mysqli_* or PDO
Remove the 's around field names -
"SELECT TOP 1 id FROM radcheck ORDER BY id DESC";
But as Daan told it will not work for mysql, then ORDER & LIMIT will do the trick.
"SELECT id FROM radcheck ORDER BY id DESC LIMIT 1";
i hope you never mind trying this pattern
Select id from radcheck WHERE id=1 ORDER BY 'id' DESC ;

Mysql select list after a specific id

I'm trying to find a solution to select a list of rows coming after a certain Id from an ordered list.
For example, first I select 1000 rows. Then, on a subsequent request, i want to fetch another 1000 rows coming from after the last id of the first request. I know i can do it with limit, but suppose there has been 100 rows added between the first and second request, there will be 100 rows that will be from the first request.
Both queries will be ordered by the date of the entries.
Here's an example of the query I thought of:
$query = "SELECT * FROM table WHERE id AFTER $id ORDER BY date DESC";
$query = "SELECT * FROM `table` WHERE `id` > '$id' ORDER BY `date` DESC LIMIT 1000";
Two ways to do this:
WHERE
"SELECT * FROM `table` WHERE `id` > '$id' ORDER BY `date` DESC LIMIT $length"
LIMIT
"SELECT * FROM `table` LIMIT $start, $length"
$query = "SELECT * FROM table WHERE id > $id ORDER BY date LIMIT 1000";
You're asking about logic, not code so here it is.
The first request selects the first 1000.
$query = "SELECT * FROM the_table ORDER BY `date` DESC LIMIT 0,1000";
NB date is a reserved word so needs escaping if you've called a column "date" which you shouldn't.
$rs=$db->selectMany($query); // replace this with however you select the rows. $rs is results set
Do stuff with PHP and save the maximum id. They may not be in order.
$maxid=0;
foreach ($rs as $r){
// whatever you need to do with your results
$maxid=max($maxid, $r->id);
}
Your subsequent select uses the last id
$query = "SELECT * FROM the_table WHERE id > $maxid ORDER BY date DESC LIMIT 0,1000";
BUT you need to take note that you're ordering by date and using id to find a breakpoint which sounds like it would cause data to be missed.
Perhaps you mean to use WHERE`date`> $maxdate? If so you can figure that out from the code given.

there is something wrong with sql query

following sql query was working fine, i don't whats wrong i did its stopped fetching records.
$data = mysql_query("SELECT * FROM product_table where pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1 ORDER BY autoid desc limit $no2,20")
or die(mysql_error());
when i remove or clause its works fine for example
$data = mysql_query("SELECT * FROM product_table ORDER BY autoid desc limit
$no2,20")
or die(mysql_error());
please have a look and let me know where i am doing mistake....
regards,
It seems,your query is OK, but when you use WHERE clause, you limit the results , so perhaps there is no recored for display, especially when you use LIMIT for starting offset and number of results.
Your query is ok but there is no record to satisfy your where part. go to your database and create some new rows with your criteria.
Try:
$data = mysql_query("SELECT * FROM product_table where (pid=$saa1 OR gpid=$saa1 OR
category_id=$saa1) ORDER BY autoid desc limit $no2,20")
or die(mysql_error());

php how to add two where in single query

i have following sql query, everything works fine but when i put "and posted_date<>$datetime" its not retrieving data as per given command.
$datetime="0000-00-00";
$data = mysql_query("SELECT * FROM product_table where category_id=$cat1 or
pid=$par or gpid=$gpar and posted_date<>$datetime
ORDER BY autoid desc limit $no2,$cacount")
or die(mysql_error());
please check is that line is ok maybe i am doing mistake somewhere where category_id=$cat1 or pid=$par or gpid=$gpar and posted_date<>$datetime
maybe i need two where one for or and another for and...
Thanks
Try to group your condition and use DATE()
SELECT *
FROM product_table
where (category_id=$cat1 or
pid=$par or gpid=$gpar) AND DATE(posted_date) <> DATE($datetime)
ORDER BY autoid desc
LIMIT $no2, $cacount

Categories