PHP在会话变量中显示一行Mysql结果的每一列——刷新时显示下一行


PHP display each column of a row of Mysql results in session variables - next row on refresh

好的,

这里太疯狂了——我想一次显示一个数组的每一行(来自Mysql查询的结果(。我把它们放在while循环中,所有的行同时显示出来。然后有人建议我放弃while循环,使用sessions。

我花了几个小时试图让这个工作-使用这个网站上的评论和PHP教程,但我非常困惑,需要帮助。

场景:

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

显示

col1(row1(

第2列(第1列(

第3列(第1列(

第4列(第1列(

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

显示

第1列(第2列(

col2(row2(

col3(row2(

第4列(第2列(

刷新/提交。。。。等等,直到所有行都被显示为止。

我相信我必须在每次刷新中使用$_SESSION变量(我尝试过(来设置像这样的变量

$_SESSION['column1']= $rownum;
$_SESSION['column2']= $rownum;
$_SESSION['column3']= $rownum;
$_SESSION['column4']= $rownum;

但是我没有正确显示列。

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

非常感谢提前

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

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

如果您还没有主键列,请将主键列添加到表中。然后您可以使用以下内容:

<?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>";
}
?>

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

首先是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;