corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » PHP MYsql query


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author PHP MYsql query
Dan Lewis
Member

Registered: 31st Jan 05
Location: Leicestershire
User status: Offline
20th Feb 09 at 10:28   View User's Profile U2U Member Reply With Quote

Another one again

I have a table the coloums ID,USER_NAME,ROLE

The results from this table are displayed on the html page. But in the table there are duplicate entries. I am not bother about the duplicates but i dont wish the user to see them.

code:

$query = "SELECT ID, USER_NAME, ROLE, FROM FOX_MASTER_ROLE_OF_USERS ORDER BY ROLE, USER_NAME";
$result = mysql_query($query) or die(mysql_error());
echo "<br />";
echo "<table>";
while($row = mysql_fetch_array($result,))
{
echo "<tr>";
echo "<td width=200>" . $row['USER_NAME']. "</td><td width=200>". $row['ROLE']. "</td width=200><td>". "<a href=javascript:confirmDelete('delete.php?id=$row[ID]')>[Delete]</a> "</td>" ;

echo "</tr>";
}
echo "</table>";



Thats the code. If i use SELECT DISTINCT, It still shows the duplicates as im all the ID's are different. But i need to remove the duplication but still pass the ID.

James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
20th Feb 09 at 15:56   View User's Profile U2U Member Reply With Quote

Well if they are actual duplicates in the table, which one do you want to see?

Say you have this:

ID = 1, User_Name = James, Role = Admin
ID = 2, User_Name = James, Role = Admin
ID = 3, User_Name = James, Role = Admin
ID = 4, User_Name = James, Role = Admin

Which one do you want to display?

Also, why do you have duplicates?
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
20th Feb 09 at 19:18   View Garage View User's Profile U2U Member Reply With Quote

You can remove ID and use SELECT DISTINCT.

If you want ID you then have the question of which ID it is that you want to display.

You would GROUP BY in this instance, and choose your aggregate function.

SELECT MIN(ID), MAX(ID), GROUP_CONTACT(ID), USER_NAME, ROLE FROM FOX_MASTER_ROLE_OF_USERS GROUP BY USER_NAME ORDER BY ROLE, USER_NAME

(I've also deleted the redundant comma after ROLE in the field list)

Note you may also alias the aggregate fields -

GROUP_CONCAT(ID) AS IDLIST

Not entirely sure I like your data structure though! Multiple records for the same user? Not ideal. If the delete operation is actually to delete all the rows which relate to that user then USER_NAME is the field on which you should do the delete, not ID. Otherwise you start hacking in loops to your delete operation just because you data is bad. Or you have the issue that if you delete once on one ID, the user name is still displayed to the user next time they run the app, so it will look like the delete operation has not been successful.
Dan Lewis
Member

Registered: 31st Jan 05
Location: Leicestershire
User status: Offline
23rd Feb 09 at 08:33   View User's Profile U2U Member Reply With Quote

Thanks for the replies. I agree yes if i do not show the duplicates then the delete key is a pretty stupid Idea. I think i may start again and add some validation before the submit to the table as that would stop the duplication and the need for an extra table.

Thanks though
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
23rd Feb 09 at 15:26   View User's Profile U2U Member Reply With Quote

Just make the Username unique...

So when the user registers, it runs a function to check availability, and if it returns False then ask for a new username from user and not add it to database.

Function IsAvailabile(Username As String) As Boolean
Dim result As Boolean = false

(Connect to DB bla bla bla)
Execute SQL query 'Select Username from Table where username=username'

if the select returns nothing then set result = true

else result = false

return result
end function


then in your code you just have:

Take user input for username

If IsAvailable(username) = true Then add it to database
else
prompt user for new username
end if

(obv convert this to your language / actually write the proper code), but you get the idea.
Dan Lewis
Member

Registered: 31st Jan 05
Location: Leicestershire
User status: Offline
23rd Feb 09 at 15:27   View User's Profile U2U Member Reply With Quote

Cant make the username unique as the username could have multiple roles assigned to it.
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
23rd Feb 09 at 15:39   View User's Profile U2U Member Reply With Quote

I don't really understand what your thing is, or what you're trying to achieve...

but why have the roles in the same table as the user list?

Have a table for users and a table for roles

User Table:
Unique ID
Username

Role Table:
Role ID
Role Name
User ID

Then you can have:

User Table
ID: 1 - Name Paul_J
ID: 2 - Name Ian
ID: 3 - Name James

and in Role Table
ID: 1 - Role: Admin - User ID: 1
ID: 2 - Role: Mod - User ID: 1
ID: 3 - Role: User - User ID: 1
ID: 4 - Role: Mod - User ID: 2
ID: 5 - Role: User - User ID: 2
ID: 6 - Role: User - User ID: 3

Paul_J is assigned role of User, Mod and Admin

Ian is Mod and User

James is just a User

If you want to do it that way...

Or just have a different column for each 'Role Type' as a boolean, so you have the user and then a tick in the admin, mod and user box...

etc
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
23rd Feb 09 at 15:58   View User's Profile U2U Member Reply With Quote

Ideally you should have 3 tables.

Role = List of roles with an ID for each.
User = List of users with an ID for each.
UserRoleMappings = Table to link the 2 together. Columns would be UserId, RoleId. (Could optionally have an ID for the role mapping too).

This way your DB is fully normalised.
Paul_J
Member

Registered: 6th Jun 02
Location: London
User status: Offline
23rd Feb 09 at 16:01   View User's Profile U2U Member Reply With Quote

yeh that's the best ^^

Especially if you want to put a lot of columns about your roles, like 'Name', 'Description', etc
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
23rd Feb 09 at 22:38   View Garage View User's Profile U2U Member Reply With Quote

Or store a decimal and do a binary conversion on it to get a load of bools.

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
mysql function help :D waynep Geek Day 2 1341
20th Jul 03 at 20:59
by waynep
 
Anyone know anything about Java Script/HTML ed Geek Day 6 987
14th Feb 04 at 18:16
by Joff
 
(my)SQL dudes Dom Geek Day 9 632
3rd May 07 at 23:50
by Dom
 
php and mysql help please bigD21 Geek Day 4 423
30th Aug 08 at 16:46
by bigD21
 

Corsa Sport » Message Board » Off Day » Geek Day » PHP MYsql query 29 database queries in 0.0108929 seconds