本文整理汇总了PHP中think\Db::getInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP Db::getInstance方法的具体用法?PHP Db::getInstance怎么用?PHP Db::getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类think\Db
的用法示例。
在下文中一共展示了Db::getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: uninstall
public function uninstall()
{
$db_config = array();
$db_config['DB_TYPE'] = C('DB_TYPE');
$db_config['DB_HOST'] = C('DB_HOST');
$db_config['DB_NAME'] = C('DB_NAME');
$db_config['DB_USER'] = C('DB_USER');
$db_config['DB_PWD'] = C('DB_PWD');
$db_config['DB_PORT'] = C('DB_PORT');
$db_config['DB_PREFIX'] = C('DB_PREFIX');
$db = Db::getInstance($db_config);
//读取插件sql文件
$sqldata = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . '/Addons/' . $this->info['name'] . '/uninstall.sql');
$sqlFormat = $this->sql_split($sqldata, $db_config['DB_PREFIX']);
$counts = count($sqlFormat);
for ($i = 0; $i < $counts; $i++) {
$sql = trim($sqlFormat[$i]);
$db->execute($sql);
//执行语句
}
//删除钩子
$Hooks = M("Hooks");
$map['name'] = array('in', 'DisplayFocus');
$res = $Hooks->where($map)->delete();
if ($res == false) {
session('addons_install_error', $Hooks->getError());
return false;
}
return true;
}
示例2: step3
public function step3()
{
if (session('step') != 2) {
$this->redirect('step2');
}
$this->display();
//连接数据库
$dbconfig = session('db_config');
$db = Db::getInstance($dbconfig);
//创建数据表
create_tables($db, $dbconfig['DB_PREFIX']);
//注册创始人帐号
$auth = build_auth_key();
$admin = session('admin_info');
register_administrator($db, $dbconfig['DB_PREFIX'], $admin, $auth);
//创建配置文件
$conf = write_config($dbconfig, $auth);
session('config_file', $conf);
if (session('error')) {
//show_msg();
} else {
session('step', 3);
$this->redirect('Index/complete');
}
}
示例3: optimize
/**
* 优化表
*/
public function optimize()
{
$tables = $_REQUEST['tables'];
if ($tables) {
$Db = Db::getInstance();
if (is_array($tables)) {
$tables = implode('`,`', $tables);
$list = $Db->query("OPTIMIZE TABLE `{$tables}`");
if ($list) {
$this->ajaxReturn(1, "数据表优化完成!");
} else {
$this->ajaxReturn(0, "数据表优化出错请重试!");
}
} else {
$list = $Db->query("OPTIMIZE TABLE `{$tables}`");
$tables_ts = substr($tables, 3);
if ($list) {
$this->ajaxReturn(1, "数据表'{$tables_ts}'优化完成!");
} else {
$this->ajaxReturn(0, "数据表'{$tables_ts}'优化出错请重试!");
}
}
} else {
$this->ajaxReturn(0, "请指定要优化的表!");
}
}
示例4: index
public function index()
{
$Db = Db::getInstance();
$list = $Db->query('SHOW TABLE STATUS');
$list = array_map('array_change_key_case', $list);
$this->assign('list', $list);
$this->display();
}
示例5: __construct
/**
* 架构函数
* @param array $options 缓存参数
* @access public
*/
public function __construct($options = array())
{
if (empty($options)) {
$options = array('table' => C('DATA_CACHE_TABLE'));
}
$this->options = $options;
$this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
$this->options['length'] = isset($options['length']) ? $options['length'] : 0;
$this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
$this->handler = \Think\Db::getInstance();
}
示例6: getSql
private function getSql($name)
{
$db_config = array();
$db_config['DB_TYPE'] = C('DB_TYPE');
$db_config['DB_HOST'] = C('DB_HOST');
$db_config['DB_NAME'] = C('DB_NAME');
$db_config['DB_USER'] = C('DB_USER');
$db_config['DB_PWD'] = C('DB_PWD');
$db_config['DB_PORT'] = C('DB_PORT');
$db_config['DB_PREFIX'] = C('DB_PREFIX');
$this->db = Db::getInstance($db_config);
//读取插件sql文件
$sqldata = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . '/Addons/' . $this->info['name'] . '/' . $name . '.sql');
$sqlFormat = $this->sql_split($sqldata, $db_config['DB_PREFIX']);
foreach ($sqlFormat as $key => $value) {
# code...
}
return $sqlFormat;
}
示例7: uninstall
public function uninstall()
{
$db_config = array();
$db_config['DB_TYPE'] = C('DB_TYPE');
$db_config['DB_HOST'] = C('DB_HOST');
$db_config['DB_NAME'] = C('DB_NAME');
$db_config['DB_USER'] = C('DB_USER');
$db_config['DB_PWD'] = C('DB_PWD');
$db_config['DB_PORT'] = C('DB_PORT');
$db_config['DB_PREFIX'] = C('DB_PREFIX');
$db = Db::getInstance($db_config);
//读取插件sql文件
$sqldata = file_get_contents('http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . '/Addons/' . $this->info['name'] . '/uninstall.sql');
$sqlFormat = $this->sql_split($sqldata, $db_config['DB_PREFIX']);
$counts = count($sqlFormat);
for ($i = 0; $i < $counts; $i++) {
$sql = trim($sqlFormat[$i]);
$db->execute($sql);
//执行语句
}
return true;
}
示例8: repair
/**
* 修复表
* @param String $tables 表名
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
public function repair($tables = null)
{
if ($tables) {
$Db = Db::getInstance();
if (is_array($tables)) {
$tables = implode('`,`', $tables);
$list = $Db->query("REPAIR TABLE `{$tables}`");
if ($list) {
$this->success("数据表修复完成!");
} else {
$this->error("数据表修复出错请重试!");
}
} else {
$list = $Db->query("REPAIR TABLE `{$tables}`");
if ($list) {
$this->mtReturn(201, "数据表'{$tables}'修复完成!", '', 'forward', U('index'));
} else {
$this->mtReturn(300, "数据表'{$tables}'修复出错请重试!");
}
}
} else {
$this->error("请指定要修复的表!");
}
}
示例9: step4
public function step4()
{
if (session('step') !== '3') {
$this->error('请按顺序安装', U('step3'));
}
session('step', '4');
session('error', false);
$this->assign('meta_title', "step4");
$this->display();
//连接数据库
$db_config = session('db_config');
$db_instance = Db::getInstance($db_config);
//创建数据表
create_tables($db_instance, $db_config['DB_PREFIX']);
//生成加密字符串
$add_chars .= '`~!@#$%^&*()_+-=[]{};:"|,.<>/?';
$auth = String::randString(64, '', $add_chars);
//生成随机数
//创建配置文件
$conf = write_config($db_config, $auth);
//根据加密字符串更新admin密码的加密结果
$new_admin_password = user_md5('admin', $auth);
$sql = <<<SQL
UPDATE `{$db_config["DB_PREFIX"]}admin_config` SET `value`='{$auth}' WHERE `name` = 'AUTH_KEY';
UPDATE `{$db_config["DB_PREFIX"]}admin_user` SET `password`='{$new_admin_password}' WHERE `id` = 1;
SQL;
$result = $db_instance->execute($sql);
if (!$result) {
$this->error('写入系统加密KEY或管理员新密码出错!');
}
if (session('error')) {
$this->error('安装出错', 'index');
} else {
$this->redirect('complete');
}
}
示例10: repair
/**
* 修复表
* @param String $tables 表名
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
public function repair($tables = null)
{
if ($tables) {
$Db = Db::getInstance();
if (is_array($tables)) {
$tables = implode('`,`', $tables);
$list = $Db->query("REPAIR TABLE `{$tables}`");
if ($list) {
$this->success(L('_REPAIR_COMPLETE_PARAM_', array('name' => '')) . L('_EXCLAMATION_'));
} else {
$this->error(L('_REPAIR_ERROR_PARAM_', array('name' => '')) . L('_EXCLAMATION_'));
}
} else {
$list = $Db->query("REPAIR TABLE `{$tables}`");
if ($list) {
$this->success(L('_REPAIR_COMPLETE_PARAM_', array('name' => $tables)) . L('_EXCLAMATION_'));
} else {
$this->error(L('_REPAIR_ERROR_PARAM_', array('name' => $tables)) . L('_EXCLAMATION_'));
}
}
} else {
$this->error(L('_REPAIR_ASSIGN_') . L('_EXCLAMATION_'));
}
}
示例11: clear
public function clear()
{
$Db = Db::getInstance();
$tables = array("wst_users_member", "wst_users_member_apply", "wst_users_member_relation", "wst_users_member_voucher_earn");
if (IS_POST) {
$rd = array('status' => -1);
foreach ($tables as $k => $v) {
$Db->query("TRUNCATE TABLE {$v}");
}
$Db->query("update wst_users_member set b_left_user_id=0, b_middle_user_id=0,b_right_user_id=0,parentId=0,recommendId=0,reaches=0,level=0");
$rd = array('status' => 1);
$this->ajaxReturn($rd);
} else {
$this->display("/clear");
}
}
示例12: repair
/**
* 修复表
* @param String $tables 表名
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
public function repair($tables = null)
{
if ($tables) {
$Db = Db::getInstance();
if (is_array($tables)) {
$tables = implode('`,`', $tables);
$list = $Db->query("REPAIR TABLE `{$tables}`");
if ($list) {
$this->mtReturn(200, "数据表修复完成!", $_REQUEST['navTabId'], false);
} else {
$this->mtReturn(300, "数据表修复出错请重试!", $_REQUEST['navTabId'], false);
}
} else {
$list = $Db->query("REPAIR TABLE `{$tables}`");
if ($list) {
$this->mtReturn(200, "数据表'{$tables}'修复完成!", $_REQUEST['navTabId'], false);
} else {
$this->mtReturn(300, "数据表'{$tables}'修复出错请重试!", $_REQUEST['navTabId'], false);
}
}
} else {
$this->error("请指定要修复的表!");
}
}
示例13: step3
public function step3()
{
$data = session('install_config');
if (!$data) {
$this->error('非法访问');
}
$field = array('DB_TYPE', 'DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PWD', 'DB_PORT', 'DB_PREFIX');
$database = array();
foreach ($field as $key) {
$database[$key] = $data[$key];
}
$db = Db::getInstance($database);
//sql字段替换
$sql = file_get_contents(MODULE_PATH . 'Data/sql.sql');
$sql = str_replace('[[DB_PREFIX]]', $data['DB_PREFIX'], $sql);
//将sql文件解析成单条语句
$ret = sql_split($sql);
//创建管理员账号
$passwordInfo = password($data['password']);
$password = $passwordInfo['password'];
$encrypt = $passwordInfo['encrypt'];
$email = trim($data['email']);
array_push($ret, "update {$data['DB_PREFIX']}admin set `username`='{$data['username']}',`password`='{$password}',`roleid`='1',`encrypt`='{$encrypt}',`email`='{$email}' where `userid`='1'");
$tip = array();
//执行情况统计
//安装进度显示
array_push($tip, array('开始安装数据库', ''));
foreach ($ret as $value) {
$value = trim($value);
if (empty($value)) {
continue;
}
if (substr($value, 0, 12) == 'CREATE TABLE') {
$name = preg_replace("/^CREATE TABLE `(\\w+)`.*/is", "\\1", $value);
$msg = "创建数据表{$name}";
if (false !== $db->execute($value)) {
array_push($tip, array($msg, '成功'));
} else {
array_push($tip, array($msg, '失败'));
}
} elseif (substr($value, 0, 11) == 'INSERT INTO') {
$name = preg_replace("/^INSERT INTO `(\\w+)`.*/is", "\\1", $value);
$msg = "写入数据到{$name}";
if (false !== $db->execute($value)) {
array_push($tip, array($msg, '成功'));
} else {
array_push($tip, array($msg, '失败'));
}
} else {
$db->execute($value);
}
}
//同步配置文件
if (APP_MODE != 'sae') {
$configFile = CONF_PATH . 'config.php';
$data = file_get_contents($configFile);
$data = preg_replace("/('DB_TYPE'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_TYPE']}',", $data);
$data = preg_replace("/('DB_HOST'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_HOST']}',", $data);
$data = preg_replace("/('DB_NAME'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_NAME']}',", $data);
$data = preg_replace("/('DB_USER'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_USER']}',", $data);
$data = preg_replace("/('DB_PWD'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_PWD']}',", $data);
$data = preg_replace("/('DB_PORT'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_PORT']}',", $data);
$data = preg_replace("/('DB_PREFIX'\\s*=>\\s*)'(.*)',/Us", "\\1'{$database['DB_PREFIX']}',", $data);
$data = preg_replace("/('report'\\s*=>\\s*)'(.*)',/Us", "\\1'{$email}',", $data);
file_put_contents($configFile, $data);
array_push($tip, array('写入配置文件', '成功'));
}
session('install_step', 4);
array_push($tip, array('安装完成', ''));
$this->assign('tip', $tip);
$this->display();
}
示例14: backup_table
/**
* @param int $type 备份类型,1:table,2:row,3:all
*/
private function backup_table($table, $type = 1, $start = 0)
{
$db = Db::getInstance();
switch ($type) {
case 1:
if (0 == $start) {
$result = $db->query("SHOW CREATE TABLE `{$table}`");
$sql = "\n";
$sql .= "-- -----------------------------\n";
$sql .= "-- 表结构 `{$table}`\n";
$sql .= "-- -----------------------------\n";
//$sql .= "DROP TABLE IF EXISTS `{$table}`;\n";
$sql .= str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', trim($result[0]['Create Table']) . ";\n\n");
if (false === $this->write($sql)) {
return false;
}
}
//数据总数
$result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`");
$count = $result['0']['count'];
break;
case 2:
//写入数据注释
if (0 == $start) {
$sql = "-- -----------------------------\n";
$sql .= "-- 表内记录 `{$table}`\n";
$sql .= "-- -----------------------------\n";
$this->write($sql);
}
//备份数据记录
$result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, 1000");
foreach ($result as $row) {
$row = array_map('addslashes', $row);
$sql = "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n";
if (false === $this->write($sql)) {
return false;
}
}
break;
case 3:
if (0 == $start) {
$result = $db->query("SHOW CREATE TABLE `{$table}`");
$sql = "\n";
$sql .= "-- -----------------------------\n";
$sql .= "-- Table structure for `{$table}`\n";
$sql .= "-- -----------------------------\n";
$sql .= "DROP TABLE IF EXISTS `{$table}`;\n";
$sql .= trim($result[0]['Create Table']) . ";\n\n";
if (false === $this->write($sql)) {
return false;
}
}
//数据总数
$result = $db->query("SELECT COUNT(*) AS count FROM `{$table}`");
$count = $result['0']['count'];
//备份表数据
if ($count) {
//写入数据注释
if (0 == $start) {
$sql = "-- -----------------------------\n";
$sql .= "-- Records of `{$table}`\n";
$sql .= "-- -----------------------------\n";
$this->write($sql);
}
//备份数据记录
$result = $db->query("SELECT * FROM `{$table}` LIMIT {$start}, 1000");
foreach ($result as $row) {
$row = array_map('addslashes', $row);
$sql = "INSERT INTO `{$table}` VALUES ('" . str_replace(array("\r", "\n"), array('\\r', '\\n'), implode("', '", $row)) . "');\n";
if (false === $this->write($sql)) {
return false;
}
}
//还有更多数据
if ($count > $start + 1000) {
return array($start + 1000, $count);
}
}
break;
}
}
示例15: step3
public function step3()
{
/* if(session('step') != 2){
$this->redirect('step2');
}*/
$this->display();
//连接数据库
$dbconfig = cookie('db_config');
$db = Db::getInstance($dbconfig);
//创建数据表
create_tables($db, $dbconfig['DB_PREFIX']);
//注册创始人帐号
$auth = build_auth_key();
$admin = session('admin_info');
register_administrator($db, $dbconfig['DB_PREFIX'], $admin, $auth);
//创建配置文件
$conf = write_config($dbconfig, $auth);
session('config_file', $conf);
if (session('error')) {
//show_msg();
} else {
session('step', 3);
echo "<script type=\"text/javascript\">setTimeout(function(){location.href='" . U('Index/complete') . "'},5000)</script>";
ob_flush();
flush();
//$this->redirect('Index/complete');
}
}