简体   繁体   English

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

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

okay, 好的,

going crazy here - i wanted to display each row of a array (from results of Mysql query) one at a time. 在这里变得疯狂-我想一次显示一个数组的每一行(来自Mysql查询的结果)。 i had them in while loop and all rows showed at once. 我把它们放在while循环中,所有行一次显示。 i was then advised to ditch the while loop and use sessions. 然后建议我放弃while循环并使用会话。

i have spent hours trying to get this to work - using comments on this site and PHP tutorials, but am very confused and need help. 我花了数小时试图使它生效-使用本网站和PHP教程上的评论,但非常困惑,需要帮助。

scenario: 场景:

eg. 例如。 Mysql query: select col1, col2, col3, col4 from MyTable. MySQL查询:从MyTable中选择col1,col2,col3,col4。 (MyTable has about 10 rows) (MyTable大约有10行)

display 显示

col1 (row1) col1(第1行)

col2 (row1) col2(第1行)

col3 (row1) col3(第1行)

col4 (row1) col4(第1行)

refresh browser (or pref - click Submit button) 刷新浏览器(或首选-单击提交按钮)

display 显示

col1 (row2) col1(第2行)

col2 (row2) col2(第2行)

col3 (row2) col3(第2行)

col4 (row2) col4(第2行)

refresh/submit .... etc etc until all rows have been displayed. 刷新/提交...等,直到所有行都已显示。

i believe i have to use $_SESSION variables - i tried - in each refresh - to set the variables like this 我相信我必须使用$_SESSION变量-我试图-在每次刷新时-像这样设置变量

$_SESSION['column1']= $rownum;

$_SESSION['column2']= $rownum;

$_SESSION['column3']= $rownum;

$_SESSION['column4']= $rownum;

but i don't get the columns displaying correctly. 但我没有正确显示列。

please help me resolve this - and if possible with code example - including the session start and refresh or whatever it may need. 请帮助我解决此问题(如果可能,还提供代码示例),包括会话的启动和刷新,或可能需要的任何内容。

Many Thanks in advance 提前谢谢了

add session_start(); 添加session_start(); before using sessions 使用会话之前

<?php

session_start();

$_SESSION['pop'] = 12;
echo $_SESSION['pop'];

?>

There's couple ways how you can do this. 有几种方法可以做到这一点。

First is being mysql offset (not getting all rows but needed part (you might have 10000 rows there - all at once will be problem)). 首先是mysql offset(不是获取所有行,而是需要的部分(您那里可能有10000行-一次都成问题))。

First example can be used if you have quite some records or you want to prepare for future as it can handle large record groups. 如果您有很多记录,或者您想为将来做准备,因为可以处理大量记录组,则可以使用第一个示例。

Second example can be used if you have small group of records or you do sort of caching of database pre-script execution. 如果您的记录组很小或对数据库前脚本执行进行某种程度的缓存,则可以使用第二个示例。

Example #1: 范例1:

MySql - MySQL-

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

php - 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;

Other would be for example array looping and storing position in $_SESSION. 其他将是例如数组循环并在$ _SESSION中存储位置。

Example #2: 范例2:

Mysql - MySQL的-

SELECT col1, col2, col3, col4 FROM MyTable

php - 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;

Note: Adjust $limit variable to suit your needs. 注意:调整$limit变量以适合您的需求。

Your specific use case ( updated ): 您的特定用例( 已更新 ):

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;

Add a primary key column to your table if you don't already have one. 如果您还没有主键列,则将其添加到表中。 You can then use the following: 然后,您可以使用以下命令:

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM