dtt27783 2012-01-17 23:28
浏览 42
已采纳

在SESSION中发布/重定向/获取并保存数据库查询

I'm new to post/redirect/get. I'm coding up the first real site that led me to discover the need for PRG. So I had written and gotten working code that did the following:

1) user enters a search string

2) we search the database and find their desired search results

3) if we found their search results successfully, we alter the database -- 
   a 'frequency of lookups' -- to indicate the user searched and found what 
   he was looking for

4) then display the results he searched on

What I found was refreshing the page in the browser resulted in the user seeing his same search results again but we incremented the 'frequency of lookup' -- the user is limited in the frequency of lookup in the database -- so if the user refreshed the page too many times, they ran through their hourly ration of database lookups. That's when I discovered Post/Redirect/Get a few days ago.

I am now going through the site and altering all pages that alter the database and display results and switching them over to PRG. Here is the new process:

1) user enters a search string

2) we search the database and find their desired search results

3) if we found their search results successfully, we alter the database -- 
   a 'frequency of lookups' -- to indicate the user searched and found 
   what he was looking for

4) PRG to a 'results' page

5) then display the results he searched on

And I ran into a problem. I need to somehow 'pass' their search results from step (2) above to the new 'results' page in step (5) that I created to implement PRG.

So my question is -- is there a 'most common' way to do this? In reading around I've seen 'save the results in a .CSV file' and also 'save the database search results in the SESSION.'

I want to save the search results in the SESSION then on my 'GET' page I added for PRG to display the result, I'll read the search results from the session variable and display them.

By that I mean I want to do this:

$result = mysql_query($query, $theDbServer);
$_SESSION['theSearchResults'] = $result.

Then on the 'display' page, read back the search results from $_SESSION['theSearchResults'] and display them using:

 $result = $_SESSION['theSearchResults'];
 $row = mysql_fetch_row($result);

Is this a normal approach? And I suspect I cannot save the raw $result in a session variable like the above -- but I'm also not sure how to put the $result above into the $_SESSION -- how is that normally done?

  • 写回答

1条回答 默认 最新

  • dongzai0020 2012-01-17 23:35
    关注

    Assuming you don't want to have the final landing page do the querying (without decrementing their quota of course), then you're going to need to use session.

    Saving to a CSV is not standard and wouldn't scale very well. I would parse the results of the query into a more user-friendly form (a simple class or list or whatever you need). I'd then store the class and not the reader into the session. You'll probably want to clear out that portion of the session when they leave the results page (especially if it's a huge amount of data).

    This is assuming you're using raw php. There are many frameworks with features for this exact case (you want to shuttle a piece of data from one page to the next).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题