【问题标题】:serialize and then unserialize mysql result object序列化然后反序列化 mysql 结果对象
【发布时间】:2012-11-04 08:59:57
【问题描述】:
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 }

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

【问题讨论】:

    标签: php mysql serialization


    【解决方案1】:
    1. mysqli 类是在类中构建的,这些类的行为不一定与您自己的类相同。
    2. 任何类型的 MySQL 结果集通常是或包含资源,即对 MySQL 服务器上当前打开的数据的引用。您不能序列化此类外部资源,因为它们现在才有效,但以后可能不会。

    简而言之:您不能序列化 MySQL 结果集资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多