Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数据库中通过TEA或文本类型列更好地存储会话数据_Php_Postgresql_Pdo - Fatal编程技术网

Php 在数据库中通过TEA或文本类型列更好地存储会话数据

Php 在数据库中通过TEA或文本类型列更好地存储会话数据,php,postgresql,pdo,Php,Postgresql,Pdo,我会将会话数据存储在数据库中,但我不确定哪种类型的列更适合使用(我将存储序列化的对象和数组)。我认为更好的方法是使用BYTEA,因为当我几天前尝试使用名称空间存储对象时,php的pdo失去了理智(从名称空间中删除)。为了解决这个问题,它将store对象转换为BYTEA列,并告诉php在绑定值时使用类型PDO::PARAM_LOB $this->sth[$name]->bindValue($key, $value, PDO::PARAM_LOB); 但此参数仅适用于BYTEA列(

我会将会话数据存储在数据库中,但我不确定哪种类型的列更适合使用(我将存储序列化的对象和数组)。我认为更好的方法是使用BYTEA,因为当我几天前尝试使用名称空间存储对象时,php的pdo失去了理智(从名称空间中删除)。为了解决这个问题,它将store对象转换为BYTEA列,并告诉php在绑定值时使用类型PDO::PARAM_LOB

  $this->sth[$name]->bindValue($key, $value, PDO::PARAM_LOB);
但此参数仅适用于BYTEA列(Postgresql)。所以选择BYTEA列类型而不是文本是个好主意???是否有兴趣知道何时使用此列类型

我将感谢您的解决方案

更新:

我将使用session\u set\u save\u处理程序函数和sessionhandler接口实现我的会话处理程序系统。它看起来像:

class SessionHandler{

   public function open($save_path, $session_name) {// some code}

   public function close() {//some code}

   public function read($id) {// some code}

   public function write($id, $data) {
   // Here i would implement mechanism to store object into database using php pdo. 
   // Here variable $data is already serialized by php session mechanism and ready to put into database. 
   // But i have unpleasantly experience storing serialized object 
   // using pdo client with namespaces thought bindParam function.

   // This experience is:
   // The serialized object (string) with namespace was cut at half, 
   // when i tried used PDO::PARAM_STR as argument in $sth->bindValue() (TEXT column i database). 
   // When to the same operation i used PDO::PARAM_LOB (BYTEA column in database) it was stored all fine.
   }

   public function destroy($id) {//some code}

   public function gc($maxlifetime) {// some code}

}

$hnadler = new SessionHandler()
session_set_save_handler($handler, true);
经历了这次之后,我不确定该用哪一种

更新:

谢谢你的cleaver回答Craig Ringer:在你的帖子之后,我决定去看看php手册中关于序列化对象的具体内容:

序列化函数

  Returns a string containing a byte-stream representation of value 
  that can be stored anywhere.

  Note that this is a binary string which may include null bytes, 
  and needs to be stored and handled as such. For example, serialize() 
  output should generally be stored in a BLOB field in a database, 
  rather than a CHAR or TEXT field.

因此,您建议将对象存储在BYTEA type列中而不是文本中是很好的。

如果您存储的是序列化的应用程序数据,
BYTEA
是合适的选择。它只是字节,除了提供它的应用程序之外,它对任何东西都没有意义,而且它不是文本,因为它不是有意义的字符

因此,出于数据建模的原因,
bytea
是您应该使用的

此外,如果数据可能包含空字节,则必须使用
bytea
,因为
text
不能包含空字节,并且将数据编码为(例如)base64来解决这一问题效率低下且可怕。

从命名空间中删除这意味着什么?如果会话信息包含名称空间的类并存储它们,那么它们当然也会包含斜杠。