} elseif ($type == 'pdo_mysql') { if (strpos($host, ':') !== FALSE) { list($host, $port) = explode(':', $host); } else { $port = 3306; } try { $link = new PDO("mysql:host={$host};port={$port};", $user, $pass); } catch (Exception $e) { $error = $e->getMessage(); message(1, $error); } $host = $host . ':' . $port; $conf['db']['type'] = 'pdo_mysql'; $conf['db']['pdo_mysql'] = array('master' => array('host' => $host, 'user' => $user, 'password' => $pass, 'name' => $name, 'charset' => 'utf8', 'engine' => 'MyISAM'), 'slaves' => array()); $db = new db_pdo_mysql($conf['db']['pdo_mysql']); $db->connect(); if ($db->errno == -10000) { $db->errno = 0; $r = $link->exec("CREATE DATABASE `{$name}`"); if ($r === FALSE) { message(-1, "尝试创建数据库失败:{$name}"); } } elseif ($db->errno != 0) { message(-1, $db->errstr); } } else { message(-1, '不支持的 type'); } $r = $db->find_one("SELECT * FROM `bbs_user` LIMIT 1"); !empty($r) and !$force and message(5, '已经安装过了。');
<?php function bbs_assert($comment, $expression) { $add = $expression ? '... [OK]' : '... [FAILDED]'; echo $comment . $add . "\t\r\n"; } define('DEBUG', 2); define('TEST_PATH', str_replace('\\', '/', getcwd()) . '/'); define('FRAMEWORK_PATH', TEST_PATH . '../'); $conf = (include TEST_PATH . 'conf.php'); include FRAMEWORK_PATH . 'core.php'; core::init($conf); echo "Test db_mysql.class.php \r\n\r\n"; $db = new db_pdo_mysql($conf['db']['mysql']); $db->query("DROP TABLE IF EXISTS `bbs_user`"); $db->query("CREATE TABLE `bbs_user` (\n `uid` int(11) unsigned NOT NULL auto_increment,\t\t# 用户id\n `regip` int(11) NOT NULL default '0',\t\t\t\t# 注册ip\n `regdate` int(11) unsigned NOT NULL default '0',\t\t# 注册日期\n `username` char(16) NOT NULL default '',\t\t\t# 用户名\n `password` char(32) NOT NULL default '',\t\t\t# 密码 md5()\n `salt` char(8) NOT NULL default '',\t\t\t\t# 随机干扰字符,用来混淆密码\n `email` char(40) NOT NULL default '',\t\t\t\t# EMAIL\n `groupid` tinyint(3) unsigned NOT NULL default '0',\t\t# 用户组 id\n `threads` mediumint(8) unsigned NOT NULL default '0',\t\t# 主题数\n `posts` mediumint(8) unsigned NOT NULL default '0',\t\t# 回帖数\n `avatar` int(11) unsigned NOT NULL default '0',\t\t# 头像最后更新的时间,0为默认头像\n KEY username(`username`),\n PRIMARY KEY (`uid`)\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;"); $db->truncate('user'); $db->maxid('user-uid', 0); $db->count('user', 0); $db->count('user-abc-123', 100); // 增加一条记录: $uid = $db->maxid('user-uid', '+1'); bbs_assert("maxid('user-uid', '+1')", $uid == 1); $r = $db->set("user-uid-{$uid}", array('username' => 'admin1', 'email' => '*****@*****.**')); bbs_assert("set()", $r == TRUE); // 增加一条记录: $uid = $db->maxid('user-uid', '+1'); bbs_assert("maxid('user-uid', '+1')", $uid == 2); $r = $db->set("user-uid-{$uid}", array('username' => 'admin2', 'email' => '*****@*****.**')); // 增加一条记录:
$db = new db_mysql($conf['db']['mysql']); } elseif ($type == 'pdo_mysql') { if (strpos($host, ':') !== FALSE) { list($host, $port) = explode(':', $host); } else { $port = 3306; } try { $link = new PDO("mysql:host={$host};port={$port};", $user, $pass); } catch (Exception $e) { $error = $e->getMessage(); message(1, $error); } $conf['db']['type'] = 'pdo_mysql'; $conf['db']['pdo_mysql'] = array('master' => array('host' => $host, 'user' => $user, 'password' => $pass, 'name' => $name, 'charset' => 'utf8', 'engine' => 'MyISAM'), 'slaves' => array()); $db = new db_pdo_mysql($conf['db']['pdo_mysql']); } else { message(-1, '不支持的 type'); } $r = $db->find_one("SELECT * FROM `bbs_user` LIMIT 1"); !empty($r) and !$force and message(5, '已经安装过了。'); !is_dir('./upload/avatar') and mkdir('./upload/avatar', 0777); !is_dir('./upload/forum') and mkdir('./upload/forum', 0777); !is_dir('./upload/attach') and mkdir('./upload/attach', 0777); $conf['auth_key'] = md5(time()) . md5(uniqid()); file_put_contents('./conf/conf.php', "<?php\r\nreturn " . var_export($conf, true) . ";\r\n?>"); write_database('./install/install.sql'); $salt = rand(100000, 999999); $pwd = md5(md5($adminpass) . $salt); $admin = array('username' => $adminuser, 'email' => $adminemail, 'password' => $pwd, 'salt' => $salt, 'create_ip' => $longip, 'create_date' => $time); user_update(1, $admin);