序列化然后反序列化mysql结果对象

时间:2012-11-04 08:59:57

标签: php mysql serialization

class a {
  public function getContinents () {
    // connect to db
    $res = $this->db->query("SELECT continent FROM mytable");
    return $res;
  }
}

$obj = new a();
$getContinents = $obj->getContinents();

因此,如果我们在这里检查变量getContinents,它是一个有效的mysqli-result对象

var_dump($getContinents);

结果是

object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(6) ["type"]=> int(0) }

现在我想序列化和反序列化这个对象

$getContinents_to_str = serialize($getContinents);
var_dump(unserialize($getContinents_to_str));

现在结果是

Warning: var_dump(): Property access is not allowed yet in ...

object(mysqli_result)#5 (5) { ["current_field"]=> NULL ["field_count"]=> NULL ["lengths"]=> NULL ["num_rows"]=> NULL ["type"]=> NULL }

请告诉我为什么会这样?哪里错了?

1 个答案:

答案 0 :(得分:7)

  1. mysqli类是在类中构建的,它们的行为不一定与您自己的类相同。
  2. 任何类型的MySQL结果集通常是或包含资源,即对MySQL服务器上当前打开的数据的引用。您无法序列化此类外部资源,因为它们现在只有效,但可能不会更晚。
  3. 简而言之:您无法序列化MySQL结果集资源。