简体   繁体   中英

Php/MySQL: Alternative for selecting all products/rows?

My PHP application has the function product_fetch([parameters]) which returns 'Product' objects which information are stored in database.

In my admin area, there is a page called "Featured Products" which allows me to select 10 products to be displayed in the main page.

Now comes the problem: I made 10 select/combobox, each allows me to select one product, out of 400. So in order to make all the options, a query has to be made: SELECT * FROM products

Question: Is it correct to make such a query, even though there's hundreds of rows?

The solution you proposed is certainly do-able, and 400 rows is really meek compared to the upper limits of what MySQL is capable of handling. What is more concerning is the user experience here. Granted this will only affect you, but I would design myself something a little nicer than a bunch of <select> s. My idea is to start with just one textbox that autocompletes the names of your products. This can be accomplished if the product title has a fulltext index . Then your autocomplete script could use this query:

SELECT * FROM Products WHERE MATCH(title) AGAINST ('contents of textbox' IN BOOLEAN MODE);

There are plenty of jQuery plugins like Autocomplete that will handle the JS side (querying the server for autocomplete results). The one I just mentioned adds a term GET parameter which you could easily grab and throw into the query:

// You might want to only select the relevant columns
$stmt = $pdo->prepare('SELECT * FROM Products WHERE MATCH(title) AGAINST (:search IN BOOLEAN MODE)');
$stmt->execute(array(':search' => $_GET['term']);

// Output JSON results
echo json_encode($stmt->fetchall());

Once you type in (or click on an autocomplete result) in the one textbox, another should appear below it and the focus should go to it. From there you can type another product, and continue until you reach 10. Each textbox (unless there is only one) should have a delete link next to it that removes that product from the featured listing.

I'll look for a site that implements this kind of functionality so you can better understand what I'm talking about. Basically here, what you're searching for is achieved. You don't have to have 10 select boxes with 400 options and you don't need a SELECT * FROM Products .

You would be much better of specifying which produts you want in the query and only returning those if you have no intention of using any of the others at all.

You can do this using many methods. A simple one would be using an ID field in an in statement like this:

 select col1, col2 from products where id in(1,4,12,5)

It might seem to make little difference, but what if your procuts table had a hundred thousand rows in it?

You could also have a flag in the table to say that the items are featured which would let you use something like this:

select col1, col2 from products where featured='Y'

Or you could even have a table that only has featured items (even just their ID) and join it to your main listing like this:

select
    a.col1,
    a.col2
from
    products a
        join featured b
            on a.id=b.id

If you want to pick through your whole table, you can even use a simple limit clause that picks up a certain number of rows from the table and can be reused to get the next set:

select col1, col2 from products limit 10;
// Will pick the first 10 rows of data.

select col1, col2 from procuts limit 30,10;
// Will pick rows 31-40 (skipping first 30, then returning next 10)

The short version is though, no matter how you do it, pulling back a whole table of data to pick through it within PHP is a bad thing and to be avoided at all costs. It makes the database work much harder, uses more network between the database and PHP (even if they are on the same machine, it is transferring a lot more data between the two of them) and it will by default make PHP use a lot more resources to process the information.

SELECT * FROM tbl LIMIT $page,10; 

The above query will select 10 entries from offset $page Fetching all rows is a bad idea as you are using 10 only anyway. When your table expands to millions of rows, you will see a noticeable difference.

There is nothing fundamentally wrong with selecting all rows if that's what you mean to do. Hundreds of rows also shouldn't be a problem for the query in terms of performance. However, thousands or millions might if your database grows that big.

Think about the user - can they scroll through hundreds of products? Probably. If not, then maybe it's the UI design at fault , not the query.

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