简体   繁体   English

在数据库中存储会话数组

[英]Store Session Array in Database

I have the array-ed session.... 我有阵列会议....

$_SESSION['Names'] = array (11,15,26);
$_SESSION['Location'] = array (35,42,10);

and I want to store them in my database... 我想将它们存储在我的数据库中......

$que = "Insert into tblpeople (DateTimePosted, first, second, third) VALUES(now(),'$_SESSION['Names'][0], $_SESSION['Location'][0])','$_SESSION['Names'][1], $_SESSION['Location'][1])','$_SESSION['Names'][2], $_SESSION['Location'][2])')";
$exec = mysql_query($que);

After Saving, my database (tblpeople) shows the following values: 保存后,我的数据库(tblpeople)显示以下值:

DateTimePosted: 2014-01-03 16:23:02 DateTimePosted: 2014-01-03 16:23:02

first: Array[0],Array[0] first: Array [0],Array [0]

second: Array[1],Array[1] 第二: 数组[1],数组[1]

third: Array[2],Array[2] 第三: 数组[2],数组[2]

Instead, I want my output to be... 相反,我希望我的输出是......

DateTimePosted: 2014-01-03 16:23:02 DateTimePosted: 2014-01-03 16:23:02

first: 11,35 第一名: 11,35

second: 15,42 第二名: 15,42

third: 26,10 第三名: 26,10

What's wrong? 怎么了?

To expand multidimensional arrays in a string, you need to wrap them in curly braces: 要在字符串中展开多维数组,需要将它们用大括号括起来:

$que = "Insert into tblpeople (DateTimePosted, first, second, third)
        VALUES(now(),
               '{$_SESSION['Names'][0]}, {$_SESSION['Location'][0]}',
               '{$_SESSION['Names'][1]}, {$_SESSION['Location'][1]}',
               '{$_SESSION['Names'][2]}, {$_SESSION['Location'][2]}')";

You also had some extra parentheses in the values. 您还在值中添加了一些括号。

However, this seems like a pretty strange way to store data into a database. 但是,这似乎是将数据存储到数据库中的一种非常奇怪的方式。 Why do you have two values separated by commas in each column, rather than splitting each into separate columns? 为什么每列中有两个以逗号分隔的值,而不是将每个值拆分为单独的列? And why are you storing array elements into different columns, rather than using separate tables with each value in a row? 为什么要将数组元素存储到不同的列中,而不是将每个值与单独的表一起使用?

use this function 使用此功能

$x=serialize($_SESSION['Names']);

it return a string that you can save any where 它返回一个字符串,你可以保存在任何地方

and this function reverse it 而这个功能扭转了它

$_SESSION['Names']=unserialize($x);

Try this 尝试这个

<?php
session_start();
$_SESSION['Names'] = array (11,15,26);
$_SESSION['Location'] = array (35,42,10);


$refNumbers = $_SESSION['Names'];
$partIds = $_SESSION['Location'];



$combined = array();

foreach($refNumbers as $index => $refNumber) {
    if(!array_key_exists($index, $partIds)) {
        throw OutOfBoundsException();
    }

    $combined[] = array(
        'Names'  => $refNumber,
        'Location' => $partIds[$index]
    );
}

print_r($combined);

$combine1 = implode(",",$combined[0]);
$combine2 = implode(",",$combined[1]);
$combine3 = implode(",",$combined[2]);



$que = "insert into tblpeople (DateTimePosted, first, second, third) VALUES(now(),'$combine1','$combine2','$combine3')";
//$exec = mysql_query($que);

?>

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

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