Sort by Username

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Sort by Username

Post by nickman013 »

Hello,

I have a database for tracking screen names that click my link.

It works.

However I was wondering what the mySQL query would be if I wanted to display just one line for each screen name.

Like right now I am just displaying all the rows and stuff in a html table. I just want to sort it out so It only views one line per screen name.

My database view
------------------------
Nicky
-----
Nicky
-----
Joe
-----
Nicky
-----
Nicky
-----
Nicky
------------------------




I want to make it look like this
------------------------
Nicky
-----
Joe
------------------------
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look into GROUP BY or SELECT DISTINCT
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thank You!
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I actually need one more thing. How would I delete all the username that are "Nicky"

My query I am running now is

Code: Select all

$row  = $_POST['Screen Name'];
$sql = "DELETE FROM `Tracker` WHERE `Screen Name` = $row;";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you need to quote $row, at a minimum. Pass it through input filtering is ideal.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

nickman013 wrote:I actually need one more thing. How would I delete all the username that are "Nicky"

My query I am running now is

Code: Select all

$row  = $_POST['Screen Name'];
$sql = "DELETE FROM `Tracker` WHERE `Screen Name` = $row;";
you can directly put $_POST variable in the query itself which would save a variable space....

Code: Select all

$sql = "DELETE FROM `Tracker` WHERE `Screen Name` = '{$_POST['Screen Name']}'";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

raghavan20 wrote:you can directly put $_POST variable in the query itself which would save a variable space....

Code: Select all

$sql = "DELETE FROM `Tracker` WHERE `Screen Name` = '{$_POST['Screen Name']}'";
Never usually a good idea and should be avoided whenever possible. Its a big security risk.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Pimptastic wrote:
raghavan20 wrote:you can directly put $_POST variable in the query itself which would save a variable space....

Code: Select all

$sql = "DELETE FROM `Tracker` WHERE `Screen Name` = '{$_POST['Screen Name']}'";
Never usually a good idea and should be avoided whenever possible. Its a big security risk.
you mean to say that I should use like this...:roll:

Code: Select all

$sql = "DELETE FROM `Tracker` WHERE `Screen Name` = '".mysql_real_escape_string(strip_tags($_POST['Screen Name']))."'";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

probably not like that...but i would sanitize all input at some point before putting that data in my query
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Pimptastic wrote:probably not like that...but i would sanitize all input at some point before putting that data in my query
can you post me an example sometime later when you are free on how you clean up unwanted add ons with the input... :)
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thank you all for resonding. Feyd you were right I justed needed to single quotes.
Post Reply