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 8:36 am, Marcelo Wolfgang wrote:
> I'm building a news display page for a website, and since the user has
> 2
> ways to arrive there, I want to know if this is possible:
>
> 1) the user arrive at news.php
>
> I will run a query at the db, and get the latest news to be the main
> one
>   (full display) and display the others news in a list
>
> 2) the user arrived from a link to a specific news to news.php?id=10
>
> It should display the news with id = 10 as the main news and get the
> latest ones to display in a list of other news
>
> I've so far was able to add a dinamic WHERE to my query ( if I have or
> not the id GET parameter ) and if I don't have it, I'm able to display
> the latest result as the main news, but when I have an id as a GET
> parameter, I have a where clause in my query and it will return only
> the
> main news and not build up the news list
>
> 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;

SQL injection attack alert:
You *really* need to sanitize this input.
http://phpsec.org/

> } 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";

Using select * is usually a bad idea anyway, but you can also add:
$newsId = (int) $newsId;
select *, auto_id = $newsId as requested from ...

This then gives PHP a way to tell if this is a story they ASKED FOR by
id, or just a story, as you have an "extra" filed called 'requested'

This assumes that you never ever have 0 as an ID in the database, as
the (int) typecast will force it to 0 if you don't have a $newsId, but
you almost for sure won't have a 0 for auto_id, as it's an
auto_increment field that starts at 1 and goes up to over 2 billion.

-- 
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