如何在php中将数组值存储在会话变量中并在以后使用

如何在php中将数组值存储在会话变量中并在以后使用,php,mysql,Php,Mysql,我想将所有id存储在会话变量中,以便以后使用 <div class="" style="padding-top: auto;"> <?php $item_id= $_SESSION['id']; $conn = mysqli_connect("localhost", "root", "", "store")or die($mysqli_error($conn)); $select_query = "SELECT * from users_items where user

我想将所有id存储在会话变量中,以便以后使用

<div class="" style="padding-top: auto;">

<?php  
$item_id= $_SESSION['id'];
$conn = mysqli_connect("localhost", "root", "", "store")or die($mysqli_error($conn));
$select_query = "SELECT  * from users_items where user_email='$email'";
$select_query_result = mysqli_query($conn, $select_query) or die(mysqli_error($conn));
?>
    <table class="table table-bordered ">
        <th >status</th>
        <?php while ($row = mysqli_fetch_array($select_query_result)) { ?>
            <th class="container-fluid"  style="float: right;"> <?php echo $row['id']; 
        ?>


        <?php } ?>
        </th>    
    </table>
</div>

将ID添加到循环中的数组中

<div class="" style="padding-top: auto;">

<?php  
$item_id= $_SESSION['id'];
$conn = mysqli_connect("localhost", "root", "", "store")or die($mysqli_error($conn));
$select_query = "SELECT  * from users_items where user_email='$email'";
$select_query_result = mysqli_query($conn, $select_query) or die(mysqli_error($conn));
?>
    <table class="table table-bordered ">
        <tr><th >status</th></tr>
        <?php while ($row = mysqli_fetch_array($select_query_result)) { ?>
            <tr><td class="container-fluid"  style="float: right;"> <?php echo $row['id']; ?> </td></tr>
        <?php 
            $item_id[] = $row['id'];
        } ?> 
    </table>
</div>
<?php 
$_SESSION['id'] = $item_id;


你应该使用事先准备好的语句。然后可以使用
array\u column()
从DB返回的行数组中过滤出id

<div class="" style="padding-top: auto;">

<?php
$item_id = $_SESSION['id'];

// enable mysqli errors
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli("localhost", "root", "", "store");
$mysqli->set_charset('utf8mb4'); // set proper connection charset

// prepare -> bind -> execute
$stmt = $conn->prepare('SELECT  * from users_items where user_email=?');
$stmt->bind_param('s', $email);
$stmt->execute();
$result = $stmt->get_result()->fetch_all(); // Fetch all rows into an array
$_SESSION['array_of_ids'] = array_column($result, 'id'); // filter out a single column and save into session
?>
    <table class="table table-bordered ">
        <th >status</th>
        <?php foreach($result as $row) { ?>
            <th class="container-fluid"  style="float: right;"> <?php echo $row['id']; ?>


        <?php } ?>
        </th>    
    </table>
</div>

地位