public function testAutoReconnect() { global $config; $dbh = new MyPDO($config['dsn'], $config['user'], $config['password']); $dbh->exec('SET session wait_timeout = 1'); sleep(2); $result = $dbh->query('SELECT 1'); }
/** * Get single instance of class * * @return MyPDO */ public static function getInstance() { if (is_null(self::$_instance)) { $dbConfig = (require_once __DIR__ . '/../dbconfig.php'); self::$_instance = new MyPDO($dbConfig); if (is_null(self::$_instance)) { self::$_instance = new MyPDO($dbConfig); } else { self::$_instance->exec("set names utf8"); } } return self::$_instance; }
protected function connect($name) { if (array_key_exists('host', $this->_connections[$name]['connection_data'])) { $host = $this->_connections[$name]['connection_data']['host']; } else { $host = 'localhost'; } if (array_key_exists('port', $this->_connections[$name]['connection_data'])) { $port = $this->_connections[$name]['connection_data']['port']; } else { $port = self::DEFAULT_MYSQL_PORT; } if (array_key_exists('user', $this->_connections[$name]['connection_data'])) { $user = $this->_connections[$name]['connection_data']['user']; } else { $user = ''; } if (array_key_exists('password', $this->_connections[$name]['connection_data'])) { $password = $this->_connections[$name]['connection_data']['password']; } else { $password = ''; } if (array_key_exists('dbname', $this->_connections[$name]['connection_data'])) { $dbname = $this->_connections[$name]['connection_data']['dbname']; } else { $dbname = null; } $this->_connections[$name]['connected'] = true; try { $dsn = "mysql:host={$host}"; if ($port !== null) { $dsn .= ";port={$port}"; } if ($dbname !== null) { $dsn .= ";dbname={$dbname}"; } $dbh = new MyPDO($dsn, $user, $password, array()); } catch (PDOException $e) { throw new DB_Connection_Failure_Exception($name); } $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); $dbh->exec('SET CHARACTER SET utf8'); $this->_connections[$name]['pdo'] = $dbh; }
/** @fn dbconn($fnConfirm=$GLOBALS["dbConfirmFn"]) @param fnConfirm fn(dbConnectionString), 如果返回false, 则程序中止退出。 @key dbConfirmFn 连接数据库前回调。 连接数据库 数据库由全局变量$DB(或环境变量P_DB)指定,格式可以为: host1/carsvc (无扩展名,表示某主机host1下的mysql数据库名;这时由 全局变量$DBCRED 或环境变量 P_DBCRED 指定用户名密码。 dir1/dir2/carsvc.db (以.db文件扩展名标识的文件路径,表示SQLITE数据库) 环境变量 P_DBCRED 指定用户名密码,格式为 base64(dbuser:dbpwd). */ function dbconn($fnConfirm = null) { global $DBH; if (isset($DBH)) { return $DBH; } global $DB, $DBCRED, $USE_MYSQL; // e.g. P_DB="../carsvc.db" if (!$USE_MYSQL) { $C = ["sqlite:" . $DB, '', '']; } else { // e.g. P_DB="115.29.199.210/carsvc" // e.g. P_DB="115.29.199.210:3306/carsvc" if (!preg_match('/^"?(.*?)(:(\\d+))?\\/(\\w+)"?$/', $DB, $ms)) { throw new MyException(E_SERVER, "bad db=`{$DB}`", "未知数据库"); } $dbhost = $ms[1]; $dbport = $ms[3] ?: 3306; $dbname = $ms[4]; list($dbuser, $dbpwd) = getCred($DBCRED); $C = ["mysql:host={$dbhost};dbname={$dbname};port={$dbport}", $dbuser, $dbpwd]; } if ($fnConfirm == null) { @($fnConfirm = $GLOBALS["dbConfirmFn"]); } if ($fnConfirm && $fnConfirm($C[0]) === false) { exit; } try { $DBH = new MyPDO($C[0], $C[1], $C[2]); } catch (PDOException $e) { $msg = $GLOBALS["TEST_MODE"] ? $e->getMessage() : "dbconn fails"; throw new MyException(E_DB, $msg, "数据库连接失败"); } if ($USE_MYSQL) { $DBH->exec('set names utf8'); } $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # by default use PDO::ERRMODE_SILENT # enable real types (works on mysql after php5.4) # require driver mysqlnd (view "PDO driver" by "php -i") $DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $DBH->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); return $DBH; }
$stack = debug_backtrace(); if (!isset($stack[1])) { return; } printf("%s(", $stack[1]['function']); $args = ''; foreach ($stack[1]['args'] as $k => $v) { $args .= sprintf("%s, ", var_export($v, true)); } if ($args != '') { printf("%s", substr($args, 0, -2)); } printf(")\n"); } } $db = new MyPDO(PDO_MYSQL_TEST_DSN, PDO_MYSQL_TEST_USER, PDO_MYSQL_TEST_PASS); $db->exec('DROP TABLE IF EXISTS test'); $db->exec('CREATE TABLE test(id INT)'); $db->exec('INSERT INTO test(id) VALUES (1), (2)'); $stmt = $db->query('SELECT * FROM test ORDER BY id ASC'); var_dump($stmt->fetchAll(PDO::FETCH_ASSOC)); var_dump($stmt->fetch()); $db->intercept_call(); } catch (PDOException $e) { printf("[001] %s [%s] %s\n", $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo())); } $db->exec('DROP TABLE IF EXISTS test'); print "done!\n"; require dirname(__FILE__) . '/mysql_pdo_test.inc'; $db = MySQLPDOTest::factory(); $db->exec('DROP TABLE IF EXISTS test');