PhP/MySQL 在页面之间存储用户搜索,以便用户可以返回到它们

发布于 2024-08-19 11:20:31 字数 438 浏览 6 评论 0原文

我正在编写一个 php-mysql 应用程序。该应用程序允许用户对数据库进行搜索。运行搜索时,如果用户在检查了搜索所带来的项目后单击后退按钮返回到搜索,系统会询问他们是否要重新发送查询(它使用 post)。这将在数​​据库中重新运行搜索。使用 get 不会导致重新发布的请求 - 但它仍然会再次运行搜索。

有没有办法存储搜索并调用它,而无需在数据库中重新运行它?

这可以将其保存在数据库中,或者以某种方式保存在 php 中 - 也许使用会话?这些搜索可能会产生大量数据,我觉得对于会话来说可能太多了。

我可以想象的另一种方法是将其存储在数据库中的某种临时表中。使用我想要查询的列创建表,然后选择插入。然后从表中选择*。这似乎也是一个坏主意。许多动态创建的临时表四处浮动。

我知道这是可以做到的。保存的搜索是您在许多网络应用程序中看到的内容。那么一般是怎么做的呢?

I'm writing a php-mysql application. The application allows the user to run a search against the database. When the search is run, if the user clicks on the back button to go back to it after examining an item brought up by it, they are asked if they would like to resend the query (it uses post). Which would rerun the search in the database. Using get wouldn't result in the request to repost - but it would still run the search again.

Is there a way to store the search and recall it, with out rerunning it in the database?

This can be saving it in the database, or somehow in the php - maybe with sessions? There can be a lot of data coming out of these searches, I feel like it's probably too much for sessions.

The other way I can imagine is to store it in the database, in a temporary table of some kind. Creating the table with the columns I intend to query, then selecting into an insert. Then selecting * from the table. This also seems rather like a bad idea. Lots of dynamically created temporary tables floating around.

I know this can be done. Saved searches are things you see in many web applications. So how is it generally done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-08-26 11:20:31

memcached

APC

但是,我注意到您没有给出任何原因为什么要缓存搜索结果。 “因为它更快,而且我读到它使我的应用程序更具可扩展性”,这是常见的原因。这听起来像是过早的优化。

我建议您构建您的应用程序并对其进行分析。这些“搜索”的成本有多大? 为什么它们的成本很高?是架构设计不好吗?索引不好吗?是不是存储引擎选择不当?

你确实意识到MySQL有一个查询缓存,正确的?在许多情况下,这可能就是您所需要的。尝试使用 mysql 客户端运行昂贵的“搜索”查询之一并记下时间,然后再次运行。看?

memcached

or

APC

However, I notice that you're not giving any reason why you want to cache the results of a search. "Because it's faster, and I read that it makes my app more scalable," is the usual reason for this. This smells like premature optimization.

I recommend you build your application and profile it. How costly are these "searches"? Why are they costly? Is it poor schema design? Is it poor indexing? Is it poor choice of storage engine?

You do realize that MySQL has a query cache, right? In many cases this may be all you need. Try running one of your expensive "search" queries using the mysql client and note the time, then run it again. See?

泛滥成性 2024-08-26 11:20:31

要修复“您要重新提交”错误,您可以将请求存储在会话或数据库中,然后立即重定向到新页面以实际执行搜索:

search.php?searchId=22

if (isset($_GET['searchId'])) {
  $search = $_SESSION['search'][$_GET['searchId'];
  // do search
} elseif (isset($_POST['search'])) {
  $_SESSION['search'][] = $_POST;
  header('Location: search.php?searchId=' . (count($_SESSION['search']) - 1);
}

然后可以从 $_SESSION 获取查询字段['search'][22] 或数据库...

至于保存结果,您可以依靠浏览器的缓存,或者您可以将单个 SERP 存储在会话或数据库中...如果需要,旧的 SERP 会在某个时候过期...

创建一个结果表很好(并且会加快复杂搜索的分页速度),但其中可能有数百万次点击,因此带有缓存的分页将是更通用的方式。

如果用户想在几天或几周后返回搜索,您只需使用保存的查询字段重新运行搜索即可。

to fix the "do you want to re-submit" error, you can store the request in the session or in the db, then immediately redirect to a new page to actually perform the search:

search.php?searchId=22

if (isset($_GET['searchId'])) {
  $search = $_SESSION['search'][$_GET['searchId'];
  // do search
} elseif (isset($_POST['search'])) {
  $_SESSION['search'][] = $_POST;
  header('Location: search.php?searchId=' . (count($_SESSION['search']) - 1);
}

that can then grab the query fields from $_SESSION['search'][22] or the db...

as for saving results, you can rely on the browser's cache, or you can store individual SERPs in the session or db... expiring out old ones at some point if needed...

creating a results table is fine (and would speed up pagination on complex searches), but you could potentially have millions of hits in there, so a pagination with caching would be more general way.

if the user wants to return to the search days or weeks later, you can just re-run the search with the saved query fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文