I need a query to search a database for a specific number within a group of numbers listed in the same column on a database.
The database Access will contain two columns
First column: Page Name
Second Column: Access
And will be set up similar to: Page Name: Blog-Admin Access: 60,61,62,76,83,94
where the numbers in the column Access are the ranks that are allowed to view the page.
On each page, there will be a query that finds the users rank and stores it in a $URank variable.
Each page is named in a variable ($PageName) which will match the column Page Name in the Access Database
In this query, it needs to search the column Access for the users rank for that page (page name = $PageName)
By storing this in a database, it will allow admins to modify who has access to what pages at any point in time.
I just need the query that will search the database to make sure that user rank has permission to access that page.
This may be very simple, not sure.
I’m use to queries such as mysql_query(“select * from database where blahblah=viarable”)…so if it could be answered in this format, that would be fantastic. Even if its the mysqli equivilant.
EDIT: Okay, it sounds like what you’re doing is storing a string of allowed ranks in the Access column. This is a very bad idea. It’s hard to manage and requires a lot of extra work in PHP. What you should do instead is use two tables, structured something like this:
So, you’d store a list of all your pages (and nothing else) in the
Pagestable. Then, add to thePage_accesstable all the possible combinations of page IDs (from thePagestable) and accepted user ranks for those pages, soPage_accessmight look something like:… etc. This means you might end up with hundreds of rows in your table—that’s okay! It’s a normalized table and if you index the columns performance will not be affected.
Alternately, if you know you’re always going to accept a rank within a range, you could structure your
Page_accesstable like this:Then just check if the user’s rank is between the min and max ranks for the page using SQL’s
between. I hope this helps with organizing your tables, but I know it still doesn’t answer your original question (how to write the SQL statements). I’d recommend making sure you’ve got a good table structure down, then ask another, more specific question once you’ve tried writing the actual queries.Original answer:
Assuming you’re using
mysqli:Something like that should do the trick. I encourage you to read up on
mysqliand prepared statements before using my example above. You really need to understand what you’re doing when writing database queries.