RE: Selecting a special row from the database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, May 4, 2007 9:26 am, Edward Kay wrote:
>> what I want is to separate the news that the user want to see ( the
>> id=XX one ) from the others rows, can someone advice me ?
>>
>> Here is the code I have so far, I hope it serve as a better
>> explanation
>> than mine!
>>
>> <?
>> $newsId = $_GET['id'];

/*
>> if (isset($newsID)){
>> 	$whereClause = 'WHERE auto_id ='.$newsId;
>> } else {
>> 	$whereClause = '';
>> }
*/


>> mysql_connect("localhost",$user,$pass) or die (mysql_error());
>> mysql_select_db ($db_table);
>> $SQL = "SELECT * FROM tb_noticias $whereClause ORDER BY auto_id
>> DESC";

Also, forget the where clause and change your ORDER BY to be:

ORDER BY auto_id = $newsId desc, auto_id desc

This will FIRST check the ID to see if it's the one you want from the
one story, then the rest in id descending format.

And you probably should not use "auto_id desc" but "postdate desc" or
whatever date field you are using to date the stories...

Technically speaking, you should NOT rely on the fact that mysql will
be adding 1 to each id to get the next one.

That happens to be how it works, and it's really unlikely to change,
but if you ever get to where you have a humongous site and a zillion
news stories in federated databases, you suddenly have this buglet
where your ID is *not* the thing you wanted to sort by.

And, in theory, the MySQL guys could change the implementation of
auto_increment out from under you, tho that's even less likely than
needing a federated db setup... :-)

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux