【问题标题】:PHP display each column of a row of Mysql results in session variables - next row on refreshPHP在会话变量中显示一行Mysql结果的每一列-刷新时的下一行
【发布时间】:2013-03-14 10:19:04
【问题描述】:

好的,

在这里发疯 - 我想一次显示一个数组的每一行(来自 Mysql 查询的结果)。我将它们放在while循环中,所有行都立即显示。然后我被建议放弃 while 循环并使用会话。

我花了几个小时试图让它工作 - 在这个网站上使用 cmets 和 PHP 教程,但我很困惑,需要帮助。

场景:

例如。 mysql查询: 从 MyTable 中选择 col1、col2、col3、col4。 (MyTable 大约有 10 行)

显示

col1 (row1)

col2 (row1)

col3 (row1)

col4 (row1)

刷新浏览器(或pref - 点击提交按钮)

显示

col1 (row2)

col2 (row2)

col3 (row2)

col4 (row2)

刷新/提交 .... 等等,直到显示所有行。

我相信我必须使用 $_SESSION 变量 - 我尝试 - 在每次刷新时 - 设置这样的变量

$_SESSION['column1']= $rownum;

$_SESSION['column2']= $rownum;

$_SESSION['column3']= $rownum;

$_SESSION['column4']= $rownum;

但我没有正确显示列。

请帮助我解决这个问题 - 如果可能的话,请提供代码示例 - 包括会话启动和刷新或任何可能需要的内容。

提前致谢

【问题讨论】:

    标签: php mysql session


    【解决方案1】:

    在使用会话之前添加session_start();

    <?php
    
    session_start();
    
    $_SESSION['pop'] = 12;
    echo $_SESSION['pop'];
    
    ?>
    

    【讨论】:

      【解决方案2】:

      如果您还没有主键列,请向您的表添加一个主键列。然后,您可以使用以下内容:

      <?php
      
      session_start();
      
      if(isset($_SESSION["id"])){
          $_SESSION["id"] = $_SESSION["id"] + 1;
      }
      else{
          $_SESSION["id"] = 1;
      }
      
      $id = $_SESSION["id"];
      
      //Your mysql connection 
      
      $results = mysql_query("SELECT * 
                              FROM urtable 
                              WHERE id = $id");
      
      while($row = mysql_fetch_array($results)){
          echo $row["urcol1"];
          echo "<br>";
          echo $row["urcol2"];
          echo "<br><br>";
      }
      
      ?>
      

      【讨论】:

      • 谢谢 - @arma's 和你的解决方案的结合让我到达了我想要去的地方。
      【解决方案3】:

      有几种方法可以做到这一点。

      首先是 mysql 偏移量(没有得到所有行但需要部分(你可能有 10000 行 - 一次都会有问题)。

      如果您有很多记录,或者您想为将来做准备,可以使用第一个示例,因为它可以处理大型记录组。

      如果您有少量记录,或者您对数据库预脚本执行进行某种缓存,则可以使用第二个示例。

      示例 #1:

      MySql -

      SELECT col1, col2, col3, col4 FROM MyTable LIMIT 4 OFFSET 0 
      

      php -

      session_start();
      
      $position = isset($_SESSION['display_position']) ? $_SESSION['display_position'] : 0;
      $limit    = 4;
      
      if(empty($position))
          $_SESSION['display_position'] = 0;
      
      $sql    = 'SELECT col1, col2, col3, col4 FROM MyTable LIMIT '.$limit.' OFFSET '.$position;
      $result = mysql_query($sql);
      
      // We don't want to fetch non existen resource
      if($result)
      {
          while($rows = mysql_fetch_array($result))
          {
              echo $rows;
          }   
      }
      else
      {
          echo 'No more records';
          exit;
      }
      
      // Update session for next run
      $_SESSION['display_position'] = $position + $limit;
      

      其他将是例如数组循环和在 $_SESSION 中存储位置。

      示例 #2:

      Mysql-

      SELECT col1, col2, col3, col4 FROM MyTable
      

      php -

      session_start();
      
      $records = array('row1', 'row2', 'row3', 'row4', 'row5', 'row6', 'row7', 'row8', 'row9', 'row10');
      
      $position = isset($_SESSION['display_position']) ? $_SESSION['display_position'] : 0;
      $limit    = 4;
      
      if(empty($position))
          $_SESSION['display_position'] = 0;
      
      if(count($records) < $position)
      {
          echo 'No more records';
          exit;
      }
      
      for($i = $position; $i < $position + $limit; $i++)
      {
          // Display rows
          if(isset($records[$i]))
              echo $records[$i];
      }
      
      // Update session for next run
      $_SESSION['display_position'] = $position + $limit;
      

      注意:调整 $limit 变量以满足您的需要。

      您的具体用例(更新):

      session_start();
      
      $records = array(
          array(
              'col1' => 'col1valuerow1',
              'col2' => 'col2valuerow1',
              'col3' => 'col3valuerow1',
              'col4' => 'col4valuerow1'
          ),
          array(
              'col1' => 'col1valuerow2',
              'col2' => 'col2valuerow2',
              'col3' => 'col3valuerow2',
              'col4' => 'col4valuerow2'
          ), 
          array(
              'col1' => 'col1valuerow3',
              'col2' => 'col2valuerow3',
              'col3' => 'col3valuerow3',
              'col4' => 'col4valuerow3'
          ), 
          array(
              'col1' => 'col1valuerow4',
              'col2' => 'col2valuerow4',
              'col3' => 'col3valuerow4',
              'col4' => 'col4valuerow4'
          ), 
          array(
              'col1' => 'col1valuerow5',
              'col2' => 'col2valuerow5',
              'col3' => 'col3valuerow5',
              'col4' => 'col4valuerow5'
          ), 
          array(
              'col1' => 'col1valuerow6',
              'col2' => 'col2valuerow6',
              'col3' => 'col3valuerow6',
              'col4' => 'col4valuerow6'
          ), 
          array(
              'col1' => 'col1valuerow7',
              'col2' => 'col2valuerow7',
              'col3' => 'col3valuerow7',
              'col4' => 'col4valuerow7'
          ), 
          array(
              'col1' => 'col1valuerow8',
              'col2' => 'col2valuerow8',
              'col3' => 'col3valuerow8',
              'col4' => 'col4valuerow8'
          ), 
          array(
              'col1' => 'col1valuerow9',
              'col2' => 'col2valuerow9',
              'col3' => 'col3valuerow9',
              'col4' => 'col4valuerow9'
          ),
          array(
              'col1' => 'col1valuerow10',
              'col2' => 'col2valuerow10',
              'col3' => 'col3valuerow10',
              'col4' => 'col4valuerow10'
          )
      );
      
      $position = isset($_SESSION['display_position']) ? $_SESSION['display_position'] : 0;
      $limit    = 1;
      
      if(empty($position))
          $_SESSION['display_position'] = 0;
      
      if(count($records) < $position)
      {
          echo 'No more records';
          exit;
      }
      
      for($i = $position; $i < $position + $limit; $i++)
      {
          // Display rows
          if(isset($records[$i])){
              echo $records[$i]['col1'] . '<br/>';
              echo $records[$i]['col2'] . '<br/>';
              echo $records[$i]['col3'] . '<br/>';
              echo $records[$i]['col4'] . '<br/>';
          }
      }
      
      // Update session for next run
      $_SESSION['display_position'] = $position + $limit;
      

      【讨论】:

      • 您好,谢谢。我很困惑 - 我之前发布了一个关于使用“while”显示我的数组并希望延迟显示直到我单击“next”或其他内容的查询,但人们告诉我我不应该使用 while 循环(或 foreach 我猜)并且应该改用会话变量...但是页面上的这些答案似乎使用了 while 或数组循环。我必须说这整个 PHP 事情很混乱!?!?!?!
      • @apetersen21 嘿,我理解你的困惑。问题是在 php 中,您使用 while 循环处理数据库记录,您也可以从 while 循环内部构建数组并使用它。在单击下一个之前停止,这将是使用 ajax 和 DOM 操作的前端技术。您对我的代码有什么顾虑?什么不适合你?这是你不打算得到的吗?
      • 嗨@arma。我对您的代码没有问题,我现在将尝试将其应用于我的实际代码。只是我很高兴使用 while 循环,因为这对我来说很有意义,但是当我发布关于在每行之间暂停 while 循环的查询(直到单击按钮)时,我被告知放弃 while 循环并使用会话反而。我想每个人都有不同的做事方式——我只需要找到适合我的方法。而while循环对我有用!我对网络开发很陌生,这对我来说是一个巨大的学习曲线。随时给我老式编程!
      • 对 - 我刚刚复制/粘贴了您的第二个示例,这绝对是可以工作的。谢谢。如果我在显示多个字段时遇到问题(即您的示例有一个数组集......我的代码将查询结果放入数组中,因此每一行也都有列。
      • @apetersen21 看看@更新的第三个例子。它会显示您可能获得的更多实时数据。
      猜你喜欢
      • 2013-07-12
      • 2017-03-09
      • 2015-12-18
      • 2013-06-05
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多