【问题标题】:Display the next record in a session php array显示会话 php 数组中的下一条记录
【发布时间】:2018-04-02 07:32:25
【问题描述】:

我正在尝试在会话变量中输出结果,该会话变量已使用 echo currentecho next 从 SQL 查询中填充。但是没有文本回显到屏幕上。

用于获取数据并放入数组的SQL语句 $SQL = "Select * from Property_Features WHERE pf_pr_ID = 3"; $result = $conn->query($SQL); $_SESSION[arrProperty_Features] = $result->fetch_assoc();

尝试回显数组中的第一项(当前)

<p><input class="w3-input" name="txtFeature1" type="text" id="txtFeature1" value="<?php echo current ($_SESSION[arrProperty_Features][pf_Feature]);?>"><br>

尝试回显数组中的第二项(下一个)
<p><input class="w3-input" name="txtFeature2" type="text" id="txtFeature2" value="<?php echo next ($_SESSION[arrProperty_Features][pf_Feature]);?>"><br>

【问题讨论】:

  • "$result->fetch_assoc()" 不会一下子给你所有的结果。继续迭代它,直到它给你 null
  • 谢谢 Tarun - 我已经尝试过了,但它似乎陷入了一个永恒的循环 while($result = $conn->query($SQL)){ $_SESSION[arrProperty_Features] = $result ->fetch_assoc(); }

标签: php mysql arrays session next


【解决方案1】:

试试下面的代码,我想你会得到想要的结果。 php 当前或下一个函数处理单个数组而不是多维数组。通过 fetch_assoc() 你将得到多维数组。

<?php
    while($row = $result->fetch_assoc()) {
        $_SESSION['arrProperty_Features'][] = $row;
    }

    ?>
    <!--Attempt to echo first item in the array-->
    <p><input class="w3-input" name="txtFeature1" type="text" id="txtFeature1" value="<?php echo $_SESSION['arrProperty_Features'][0]['pf_Feature'];?>"><br>

    <!--Attempt to echo second item in the array !-->
    <p><input class="w3-input" name="txtFeature1" type="text" id="txtFeature2" value="<?php echo $_SESSION['arrProperty_Features'][1]['pf_Feature'];?>"><br>

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    相关资源
    最近更新 更多