简体   繁体   中英

PHP/MySQL Search code and logic for relational database schema

I have created this database schema and with help from several users on here, I have a database which takes user submitted business entries stored in the business table, which are additionally grouped under one or several of about 10 catagories from the catagories table, in the tbl_works_catagories table by matching the bus_id to the catagory id.

For example, bus_id 21 could be associated with catagory_id 1, 2, 5, 7, 8.

CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT, 
`bus_name` VARCHAR(50) NOT NULL, 
`bus_dscpn` TEXT NOT NULL, 
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL, 
`category_id` INT NOT NULL
)

Now, what i want to do next is a search function which will return businesses based on the catagory. For example, say one of the businesses entered into the business table is a bakers and when it was entered, it was catagorised under Food (catagory_id 1) and take-away (catagory_id 2).

So a visitor searches for businesses listed under the Food catagory, and is returned our friendly neighbourhood baker.

As with all PHP/MySQL, i just can't (initially anyway) get my head around the logic, never mind the code!

Maybe you want something like this:

SELECT `bus_id` FROM `tbl_works_categories` WHERE `category_id` = *some id from the search* 
        AND `category_id` = *some other id from the search*;

Although you'd need those ids- there are a few ways to do this, I'll describe probably the most straight forward...

You get categories from $_POST, so let's just say you have 2 of them entered. (Food, and take-away). Parse these however you want, there are multiple ways, but the point is they're coming from $_POST.

execute this sort of thing for each one you find:

SELECT `category_id` FROM `categories` WHERE `category_name` LIKE '%*the name from $_POST*%';

Store these results in an array...based on how many you have there you can build an applicable query similar to the one I describe first. (Keep in mind you don't need and AND there, that's something you have to detect if you return > 1 category_id from the second query here)

I'm not going over things like security..always be careful when executing queries that contain user submitted data.

An alternate solution might involve a join, not too sure what that'd look like off the top of my head.

Good luck.

You should setup foreign keys in your tables to link them together.

CREATE TABLE `business` (
`bus_id` INT NOT NULL AUTO_INCREMENT, 
`bus_name` VARCHAR(50) NOT NULL, 
`bus_dscpn` TEXT NOT NULL, 
`bus_url` VARCHAR(255) NOT NULL,
PRIMARY KEY (`bus_id`)
)

CREATE TABLE `categories` (
`category_id` INT NOT NULL AUTO_INCREMENT, 
`category_name` VARCHAR(20) NOT NULL, 
PRIMARY KEY (`category_id`)
)

CREATE TABLE `tbl_works_categories` (
`bus_id` INT NOT NULL, 
`category_id` INT NOT NULL,
FOREIGN KEY (`bus_id`) REFERENCES business(`bus_id`),
FOREIGN KEY (`category_id`) REFERENCES categories(`category_id`)
)

Then your search query would be something like:

SELECT b.*
FROM business b, categories c, tbl_works_categories t
WHERE 
   b.bus_id = t.bus_id AND
   c.category_id = t.category_id AND
   c.category_id = *SOME SEARCH VALUE*

which using JOIN would be written as:

SELECT b.*
FROM business b
  JOIN tbl_works_categories t
    ON b.bus_id = t.bus_id
  JOIN categories c
    ON c.category_id = t.category_id
WHERE c.category_id = *SOME SEARCH VALUE*

If you want all businesses that are related to the given category-id, your SQL-statement would look something like this:

SELECT `business`.`bus_name`
FROM `business`
WHERE `business`.`bus_id` = `tbl_works_categories`.`bus_id`
  AND `categories`.`category_id` = `tbl_works_categories`.`category_id`
  AND `categories`.`category_id` = 1;

Where 1 in this case is your food-category, but could be your PHP variable where the ID of the category the user selected is stored.

And one hint: Be sure to name your tables either in plurar or in singular. You are mixing both and could get confused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM