1--TEST-- 2MySQL PDOStatement->fetch(), PDO::FETCH_SERIALIZE 3--EXTENSIONS-- 4pdo_mysql 5--SKIPIF-- 6<?php 7require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 8MySQLPDOTest::skip(); 9?> 10--FILE-- 11<?php 12 require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 13 $db = MySQLPDOTest::factory(); 14 15 try { 16 17 class myclass implements Serializable { 18 19 public function __construct($caller = null) { 20 printf("%s(%s) - note that it must not be called when unserializing\n", __METHOD__, var_export($caller, true)); 21 } 22 23 public function __set($prop, $value) { 24 printf("%s(%s, %s)\n", __METHOD__, var_export($prop, true), var_export($value, true)); 25 $this->{$prop} = $value; 26 } 27 28 public function serialize() { 29 printf("%s()\n", __METHOD__); 30 return 'Value from serialize()'; 31 } 32 33 public function unserialize($data) { 34 printf("%s(%s)\n", __METHOD__, var_export($data, true)); 35 } 36 37 } 38 39 printf("Lets see what the Serializeable interface makes our object behave like...\n"); 40 $obj = new myclass('Called by script'); 41 $tmp = unserialize(serialize($obj)); 42 var_dump($tmp); 43 44 printf("\nAnd now magic PDO using fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE)...\n"); 45 $db->exec(sprintf('CREATE TABLE test_stmt_fetchserialize_simple(myobj BLOB) ENGINE=%s', MySQLPDOTest::getTableEngine())); 46 $db->exec("INSERT INTO test_stmt_fetchserialize_simple(myobj) VALUES ('Data fetched from DB to be given to unserialize()')"); 47 48 $stmt = $db->prepare('SELECT myobj FROM test_stmt_fetchserialize_simple'); 49 $stmt->execute(); 50 $rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('Called by PDO')); 51 var_dump($rows[0]); 52 53 $stmt->execute(); 54 $rows = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass'); 55 var_dump($rows[0]); 56 57 printf("\nAnd now PDO using setFetchMode(PDO::FETCH:CLASS|PDO::FETCH_SERIALIZE) + fetch()...\n"); 58 $stmt = $db->prepare('SELECT myobj FROM test_stmt_fetchserialize_simple'); 59 $stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, 'myclass', array('Called by PDO')); 60 $stmt->execute(); 61 var_dump($stmt->fetch()); 62 63 } catch (PDOException $e) { 64 printf("[001] %s [%s] %s\n", 65 $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); 66 } 67 68 print "done!\n"; 69?> 70--CLEAN-- 71<?php 72require_once __DIR__ . '/inc/mysql_pdo_test.inc'; 73$db = MySQLPDOTest::factory(); 74$db->exec('DROP TABLE IF EXISTS test_stmt_fetchserialize_simple'); 75?> 76--EXPECTF-- 77Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d 78Lets see what the Serializeable interface makes our object behave like... 79myclass::__construct('Called by script') - note that it must not be called when unserializing 80myclass::serialize() 81myclass::unserialize('Value from serialize()') 82object(myclass)#%d (0) { 83} 84 85And now magic PDO using fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE)... 86 87Deprecated: PDOStatement::fetchAll(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d 88myclass::unserialize('Data fetched from DB to be given to unserialize()') 89object(myclass)#%d (0) { 90} 91 92Deprecated: PDOStatement::fetchAll(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d 93myclass::unserialize('Data fetched from DB to be given to unserialize()') 94object(myclass)#%d (0) { 95} 96 97And now PDO using setFetchMode(PDO::FETCH:CLASS|PDO::FETCH_SERIALIZE) + fetch()... 98 99Deprecated: PDOStatement::setFetchMode(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d 100 101Deprecated: PDOStatement::fetch(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d 102myclass::unserialize('Data fetched from DB to be given to unserialize()') 103object(myclass)#%d (0) { 104} 105done! 106