我的allin表具有这样的序列化数据和年份
<pre>
+------+--------+------------------------------------------------+
| id | year | data |
+------+--------+------------------------------------------------+
| 1 | 2017 | a:2:{s:3:"foo";s:1:"1";s:3:"bar";s:1:"2";} |
+------+--------+------------------------------------------------+
| 2 | 2018 | a:2:{s:3:"foo";s:1:"3";s:3:"bar";s:1:"4";} |
+------+--------+------------------------------------------------+
from:
array("foo"=>"1","bar"=>"2"); //2017
array("foo"=>"3","bar"=>"4"); //2018
</pre>
这是我的代码
<?php
$year = date("Y");
$sql = "select * from allin";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$year = $row["year"];
$db = $row["data"];
$data = unserialize($db);
//print_r($data);
}
} else {
echo "0 results";
}
echo $data["foo"]; //how about selecting 2017 or 2018???
$conn>close();
?>
在2018年和2017年如何选择foo和bar?
谢谢
答案 0 :(得分:2)
您可以更改
$data = unserialize($db);
到
$data[$row["year"]] = unserialize($db);
通过
访问echo $data[2017]["foo"];