Re: Counting down

From: Jerry Stuckle <jstucklex_at_attglobal.net>
Date: Tue, 29 Aug 2017 09:46:41 -0400
Message-ID: <oo3qvq$np8$1_at_jstuckle.eternal-september.org>


On 8/28/2017 2:56 AM, bit-naughty_at_hotmail.com wrote:
> On Tuesday, August 22, 2017 at 9:42:22 PM UTC+5:30, Jerry Stuckle wrote:

>>> I have a DB, say 10 rows, with a primary key, numbered 1 to 10. Now, according to a PHP variable I have, say $i, I would like to fetch the 10th - $i field in the DB, ie. if $i is 3, then I wanna retrieve the 7th field in the DB. (it'll have to be sorted in DESC order, right?)
>>> How do I word the SQL statement, putting the $i into it? The quoting and stuff confuses me.... :)
>>>
>>>
>>> Thanks.
>>>
>>
>> Forget TNP's "answer".  It's his normal crap answer and only works if
>> you have exactly 10 rows and the primary keys are 1-10.
>>
>> What you need is the LIMIT clause, i.e.
>>
>> SELECT col1, col2, col3 FROM mytable ORDER BY keycol DESC LIMIT 3,1;
>>
>> Play with the first LIMIT value to get the exact row you want.

>
>
> Yes, and what would the *PHP* line look like? - I get confused with the quoting, as I said.
>
> $query=sprintf("select myfields from mytable order by keycol desc limit $i,1");
>
> - is that it?
>

That is off topic in a MySQL newsgroup. You need to be asking in a PHP newsgroup.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex_at_attglobal.net
==================
Received on Tue Aug 29 2017 - 15:46:41 CEST

Original text of this message