【问题标题】:how to check in mysql which serialize array is contain value that i want to find如何在mysql中检查哪个序列化数组包含我想要查找的值
【发布时间】:2015-02-05 10:48:22
【问题描述】:

我使用 php 序列化函数在 mysql 中存储了许多数组。

但问题是, 我怎样才能找到包含序列化数组的任何一个值。

我很困惑如何获取包含我的值的数组。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>

<form method="get" action="test1.php">
<input type="text" name="first_singer"/><br>
<input type="text" name="second_singer"/><br>

<input type="submit" name="submit">

</form>
<?php
if(isset($_GET['submit'])){

$singer1=$_GET['first_singer'];
$singer2=$_GET['second_singer'];

$singer=array($singer1,$singer2);

$array=serialize( $singer );

$conn=mysqli_connect("localhost","root","","test2") or die();;
$array=serialize($singer);
$sql = "INSERT INTO `table` (`column`) VALUES ('$array')";
if(mysqli_query($conn,$sql)){
	echo 'query susseful';
	}
}
?>



</body>
</html>

【问题讨论】:

  • 更好的方法是将数据保存到规范化数据库中,而不是序列化值。正如您现在看到的,从序列化数据中读取值需要付出巨大的努力。
  • 我不知道规范化。所以给我最好的方法
  • 您自己对规范化的研究结果如何?

标签: php mysql arrays serialization


【解决方案1】:

首先,也许你必须改变你的方法。

您应该考虑为您的数据创建适当的表架构,而不是存储序列化数组。如果你真的需要存储这种数据结构,你可能不得不考虑使用某种 NoSQL 数据库,比如 MongoDB,你可以在其中将这种结构存储为 JSON。

但是可以访问您的数据,但它很脏。您可以尝试使用 MySQL Like 匹配您的项目。

例如,您有一个包含电子邮件和属于该名称的产品的数组,您可以执行以下操作:

从您的表中选择 * 其中 serialized_item like '%my@email.com%';

然后您可以反序列化该数组并处理该项目。

但正如我所说,这不是一个好主意!

【讨论】:

    【解决方案2】:
    $array=serialize( $singer );
    
    $conn=mysqli_connect("localhost","root","","test2") or die();;
    $array=serialize($singer);
    

    将这些行替换为

    $array=serialize( $singer );
    
    $conn=mysqli_connect("localhost","root","","test2") or die();;
    

    从表中选择值后使用unserialize() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2021-02-08
      • 2017-11-02
      • 1970-01-01
      • 2016-07-13
      相关资源
      最近更新 更多