本文整理汇总了PHP中db::connection方法的典型用法代码示例。如果您正苦于以下问题:PHP db::connection方法的具体用法?PHP db::connection怎么用?PHP db::connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类db
的用法示例。
在下文中一共展示了db::connection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get
public static function get($new_connection = false)
{
if (!isset(self::$config)) {
self::$config = server_get_config();
}
if (!db::$connection || $new_connection) {
$db = new self(self::$config['db_server'], self::$config['db_username'], self::$config['db_password'], self::$config['db_database'], self::$config['db_port']);
if (mysqli_connect_error()) {
throw new Exception(sprintf('Could not connect to database server. Error received: %s', mysqli_connect_error()), MYSQL_CONNECT_ERROR);
}
if (!$db->set_charset('utf8')) {
throw new Exception('Could not change MySQL character-set. Check your MySQL user credentials');
}
if (!$db->set_time_zone()) {
throw new Exception('Could not change MySQL time-zone. Check your MySQL user credentials');
}
if (isset($config['mysql_big_selects']) && $config['mysql_big_selects'] === true) {
if (!$db->enable_compat_mode()) {
throw new Exception('Could not change MYSQL compatbility options. Check your MySQL user permissions.');
}
}
if ($new_connection) {
return $db;
}
db::$connection = $db;
}
return db::$connection;
}
示例2: ping
public static function ping() {
if(!self::$connection->ping()) {
self::$connection->close();
self::$connection = NULL;
self::init();
}
}
示例3: removeEnvironment
public function removeEnvironment($removeId)
{
$db = new db();
$connection = $db->connection();
mysql_select_db($db::DBNAME);
$sqlUpdateEnvs = "UPDATE envs_environments SET for_remove=1 WHERE project_id=" . $removeId . ";";
$queryUpdateEnvs = mysql_query($sqlUpdateEnvs, $connection);
if (!$queryUpdateEnvs) {
return json_encode(array('error' => die('Could not enter data: ' . mysql_error())));
}
/*
$sqlSelectEnvs = "SELECT name, path, dbname FROM envs_environments WHERE project_id=" . $removeId . ";";
$querySelectEnvs = mysql_query($sqlSelectEnvs, $connection);
$arraySelectEnvs = array();
while ($envsRow = mysql_fetch_array($querySelectEnvs, MYSQLI_ASSOC)) {
//$arraySelectEnvs[] = ('name' => $envsRow['name'], 'path' => $envsRow['path'], 'dbname' => $envsRow['dbname']);
$arraySelectEnvs[] = $envsRow;
}
print_r($arraySelectEnvs);
*/
//return json_encode(array('return' => $arraySelectEnvs ));
/*
$sqlRemoveProject = "DELETE FROM envs_project WHERE id=" . $removeId . ";";
$queryRemoveProject = mysql_query($sqlRemoveProject, $connection);
if(!$queryRemoveProject) {
return json_encode(array('error' => die('Could not enter data: ' . mysql_error())));
}
*/
}
示例4: db
function db($table = FALSE, $orm = FALSE, $connection = 'default')
{
// If no table given, return connection
if (!$table) {
return db::connection($connection);
}
// Otherwise, return table
return $orm ? db::connection($connection)->table($table)->orm($orm) : db::connection($connection)->table($table);
}
示例5: __construct
/** A private constructor; prevents direct creation of object
*
* @param string $db
*/
private function __construct()
{
// Connecting to database, and restore link in self::$connection
$dsn = $this->DBMS . ':dbname=' . $this->db_default . ';host=' . $this->HOST . ';port=' . $this->PORT;
try {
self::$connection = new PDO($dsn, $this->USER, $this->PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die('Connection to database failed :(');
}
}
示例6: getInstance
/**
*
* Return DB instance or create intitial connection
*
* @return object (PDO)
*
* @access public
*
*/
public static function getInstance($DB_DSN, $DB_USERNAME, $DB_PASSWORD)
{
if (!self::$connection) {
try {
self::$connection = new PDO($DB_DSN, $DB_USERNAME, $DB_PASSWORD);
self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die('Can\'t connect to AMS Database');
} catch (Exception $exc) {
die('Can\'t connect to Database');
}
}
return self::$connection;
}
示例7: detail_dbtable
function detail_dbtable($app)
{
$db = db::connection();
$platform = $db->getDatabasePlatform()->getName();
if ($platform == 'mysql') {
$tables = $db->executeQuery('SHOW TABLE STATUS like ' . $db->quote($app . '%'))->fetchAll();
return view::make('dev/dbtable_mysql.html', compact('tables', 'app', 'platform'));
} else {
$tables = array_reduce($db->getSchemaManager()->listTableNames(), function ($tableList, $tableName) use($app) {
if (starts_with($tableName, $app . '_')) {
$tableList[] = ['Name' => $tableName];
}
return $tableList;
}, []);
return view::make('dev/dbtable_other.html', compact('tables', 'platform'));
}
}
示例8: testConnection
public function testConnection()
{
$this->assertInstanceOf('Database', db::connection());
}
示例9: envNameCheck
public function envNameCheck()
{
$db = new db();
$connection = $db->connection();
mysql_select_db($db::DBNAME);
$sqlGetName = "SELECT name FROM envs_project";
$queryGetName = mysql_query($sqlGetName, $connection);
$arrayGetName = array();
while ($row = mysql_fetch_array($queryGetName, MYSQL_ASSOC)) {
$arrayGetName[] = $row['name'];
}
mysql_close($connection);
return $arrayGetName;
}
示例10: db
<?php
require '../db.php';
$db = new db();
$connection = $db->connection();
mysql_select_db($db::DBNAME);
$sql = "SELECT envs_environments.type, envs_environments.path, envs_environments.domain, envs_environments.dbname, envs_environments.version, envs_config.db_host, envs_config.db_user, envs_config.db_pass, envs_config.db_name, envs_config.src_path \n\t\t\t\t\t\tFROM envs_environments \n\t\t\t\t\t\tLEFT JOIN envs_config ON envs_config.id=envs_environments.config_id \n\t\t\t\t\t\tWHERE envs_environments.is_created=0";
$result = mysql_query($sql, $connection);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
$envType = $row[0];
$envPath = $row[1];
$envDomain = $row[2];
$envDbname = $row[3];
$envVersion = $row[4];
$dbHost = $row[5];
$dbUser = $row[6];
$dbPass = $row[7];
$dbName = $row[8];
$srcPath = $row[9];
system('bash createFiles.sh envtype__' . $envType . ' envpath__' . $envPath . ' envdbname__' . $envDbname . ' envdomain__' . $envDomain . ' envversion__' . $envVersion . ' dbhost__' . $dbHost . ' dbuser__' . $dbUser . ' dbpass__' . $dbPass . ' dbname__' . $dbName . ' srcpath__' . $srcPath, $return);
}
示例11: close
/**
* uzavre vsechny nebo jenom konkretni pripojeni do db
* @param string $conn_name
* @return boolean
*/
public static function close($conn_name = null)
{
//pokud neni conn_name zadefinovany, tak uzavreme vsechna spojeni
if (is_null($conn_name)) {
foreach (self::$connectionsLink as $connName => $connLink) {
if (mysql_close($connLink) or !mysql_ping($connLink)) {
self::$connectionsLink[$conn_name] = null;
}
}
self::$connection = null;
return true;
//jinak uzavreme pouze to konkretni
} else {
//pokud neexistuje, tak uz to zrejme nekdo uzavrel pred nami
if (!isset(self::$connectionsLink[$conn_name])) {
return true;
}
//pokud se povede mysql uzavreni, tak smazeme z tabulek link
if ($tmp = mysql_close(self::$connectionsLink[$conn_name]) or !mysql_ping(self::$connectionsLink[$conn_name])) {
self::$connectionsLink[$conn_name] = null;
if ($conn_name == self::$actualConnectionName) {
self::$connection = null;
}
}
return $tmp;
}
}
示例12: db
function __construct()
{
require_once 'db.php';
$db = new db();
$this->conn = $db->connection();
}
示例13: testaInsertSerialize
public function testaInsertSerialize()
{
$accountShopModel = app::get('sysshop')->model('account');
$data = array('login_account' => 'xinxin', 'createtime' => '1432187042', 'modified_time' => '1432187042', 'login_password' => '$2y$10$kAuKY2zfMiseYU9s6ejS.OSpmcZswlkP0dy6hFE');
$sellerId = $accountShopModel->insert($data);
var_dump($sellerId);
exit;
echo 99;
$dbtable = new base_application_dbtable();
$schema = $dbtable->detect('base', 'apps')->getCreateTableSql();
var_dump($schema);
exit;
/*
$db = db::connection();
$db1 = db::connection('test');
$platform = $db->getDatabasePlatform();
$platform1 = $db1->getDatabasePlatform();
$schema = $db->getSchemaManager()->createSchema();
$schema1 = $db1->getSchemaManager()->createSchema();
$comparator = new \Doctrine\DBAL\Schema\Comparator();
$schemaDiff = $comparator->compare($schema, $schema1);
var_dump($schemaDiff->toSql($platform));
exit;
var_dump($schema->toSql($platform));
exit;
*/
$dbtable = new base_application_dbtable();
$schema = $dbtable->detect('base', 'apps')->getCreateTableSql();
var_dump($schema);
exit;
// var_dump($dbinfo->toSql(db::connection()->getDatabasePlatform()));
//----
$platform = db::connection()->getDatabasePlatform();
$db1 = db::connection('test');
$platform1 = $db1->getDatabasePlatform();
$schemaManager = $db1->getSchemaManager();
// var_dump($db1->getSchemaManager()->createSchemaConfig());exit;
$schema1 = new \Doctrine\DBAL\Schema\Schema([$schemaManager->listTableDetails('base_apps')], [], $db1->getSchemaManager()->createSchemaConfig());
$comparator = new \Doctrine\DBAL\Schema\Comparator();
$schemaDiff = $comparator->compare($schema1, $schema);
var_dump($schemaDiff->toSql($platform));
exit;
// var_dump($dbinfo);exit;
exit;
$define = ['precision' => 10, 'aa' => 2, 'fixed' => 'dddd', 'fff'];
// var_dump(array_merge($a, $b));
exit;
var_dump(array_intersect_key($a, array_flip(['precision', 'scale', 'fixed'])));
exit;
$app_id = 'base';
$db = kernel::database();
//$rows = $db->select(sprintf("show tables like '%s'", $app_id.'\_%'));
$rows = $db->select("show tables like 'base%'", $app_id . '\\_%');
var_dump($rows);
exit;
$app = 'base';
$db = app::get('base')->database();
$rows = $db->executeQuery('SHOW TABLE STATUS like ' . $db->quote($app . '\\_%'))->fetchAll();
var_dump($rows);
exit;
$app = 'base';
$db = app::get('base')->database();
//$tables = $db->executeQuery('SHOW TABLE STATUS like '. $db->quote($app.'%'))->fetchAll();
$tables = $db->executeQuery('SHOW TABLE STATUS like ?', [$app . '%'], [\PDO::PARAM_STR])->fetchAll();
var_dump($tables);
exit;
$time = time();
$db = app::get('base')->database();
$count = $db->executeQuery('SELECT count(*) FROM base_kvstore WHERE ttl>0 AND (dateline+ttl)<?', [$db->quote($time, \PDO::PARAM_INT)])->fetchColumn();
var_dump($count);
exit;
$qb = app::get('sysstat')->database()->createQueryBuilder();
$catId = 'asdf';
echo $qb->getConnection()->delete('syscategory_cat', ['cat_id' => $catId], [\PDO::PARAM_INT]);
exit;
$file = 'wapmall/index.html';
$slots['wapmall/index.html'] = array(120, 121);
$model = app::get('site')->model('widgets_instance');
// $rows = $model->database()->executeQuery('select * from site_widgets_instance where widgets_id not in(?) and core_file=?', [$slots[$file], $file], [\Doctrine\DBAL\Connection::PARAM_INT_ARRAY, \PDO::PARAM_STR])->fetchAll();
echo $model->database()->delete('site_widgets_instance', ['core_file' => $file]);
exit;
var_dump($rows);
exit;
$sTheme = 'a';
$flag = app::get('site')->database()->executeUpdate('delete from site_widgets_instance where core_file like ?', [$sTheme . '%']);
var_dump($flag);
exit;
var_dump(app::get('image')->database()->executeUpdate('update image_image SET last_modified = last_modified + 1'));
exit;
$tids = [10];
$status = 'WAIT_SELLER_SEND_GOODS';
$db = app::get('systrade')->database();
return $db->executeQuery('SELECT count(*) as ready_send_trade ,O.shop_id as shop_id ,sum(O.payment) as ready_send_fee FROM
systrade_trade as O where O.tid in (?) and O.status=? group by shop_id', [$tids, $status], [\Doctrine\DBAL\Connection::PARAM_INT_ARRAY])->fetchAll();
exit;
$db = app::get('systrade')->database();
var_dump(\Doctrine\DBAL\Connection::PARAM_INT_ARRAY);
//.........这里部分代码省略.........
示例14: database
function database()
{
//检测mysql函数库是否可用
$rst = function_exists('mysql_connect') && function_exists('mysql_get_server_info');
echo $rst ? app::get('dev')->_(" MySQL函数库可用...") . "\n" : app::get('dev')->_('MySQL函数库未安装...') . "\n";
//检测mysql数据库连接
if (!$rst) {
echo app::get('dev')->_(" MySQL函数库连接出错...");
} else {
$username = config::get('database.username');
$host = config::get('database.host');
$password = config::get('database.password');
$database = config::get('database.database');
$rst = false;
if (!is_null($host)) {
if (!is_null($password)) {
$rs = mysql_connect($host, $username, $password);
} elseif (!is_null($username)) {
$rs = mysql_connect($host, $username);
} else {
$rs = mysql_connect($host);
}
$db_ver = mysql_get_server_info($rs);
} else {
$sock = get_cfg_var('mysql.default_socket');
if (PHP_OS != 'WINNT' && file_exists($sock) && is_writable($sock)) {
$host = $sock;
} else {
$host = ini_get('mysql.default_host');
$port = ini_get('mysql.default_port');
if (!$host) {
$host = '127.0.0.1';
}
if (!$port) {
$port = 3306;
}
$host = $host . ':' . $port;
}
}
if (!$db_ver) {
if (substr($host, 0, 1) == '/') {
$fp = @fsockopen("unix://" . $host);
} else {
if ($p = strrpos($host, ':')) {
$port = substr($host, $p + 1);
$host = substr($host, 0, $p);
} else {
$port = 3306;
}
$fp = @fsockopen("tcp://" . $host, $port, $errno, $errstr, 2);
}
if (!$fp) {
$db_ver = '无法连接';
} else {
fwrite($fp, "\n");
$db_ver = fread($fp, 20);
fclose($fp);
if (preg_match('/([2-8]\\.[0-9\\.]+)/', $db_ver, $match)) {
$db_ver = $match[1];
$rst = version_compare($db_ver, '4.0', '>=');
} else {
$db_ver = '无法识别';
}
}
} else {
$rst = version_compare($db_ver, '4.1', '>=');
}
if ($db_ver == '无法连接') {
$error_msg = ' Mysql数据库无法连接...';
} elseif ($db_ver == '无法识别') {
$error_msg = ' Mysql数据库版本无法识别...';
} else {
$error_msg = ' Mysql数据库版本是' . $db_ver . ',如果版本过低,请使用高于4.1的版本...';
}
echo app::get('dev')->_($error_msg) . "\n";
}
//检测mysql数据库是否可写可读
$db = db::connection();
if ($db->getSchemaManager()->tablesExist('test')) {
$db->getSchemaManager()->dropTable('test');
}
//$db->exec('drop table if exists test')."\n";
$schema = new \Doctrine\DBAL\Schema\Schema();
$table = $schema->createTable('test');
$table->addColumn('id', 'integer', ['unsigned' => true, 'notnull' => true, 'autoincrement' => true]);
$table->addColumn('test', 'string', ['fixed' => true, 'notnull' => true]);
$table->setPrimaryKey(['id']);
$sql = current($schema->toSql($db->getDatabasePlatform()));
$db->exec($sql);
if ($db->getSchemaManager()->tablesExist('test')) {
echo app::get('dev')->_(" Mysql创建测试表正常...") . "\n";
} else {
echo app::get('dev')->_(" Mysql创建测试表不正常...") . "\n";
}
$db->createQueryBuilder()->insert('test')->values(['id' => 1, 'test' => 'test'])->execute();
if ($db->executeQuery('select id from test where id=1')->fetchColumn() == 1) {
echo app::get('dev')->_(" Mysql插入数据正常...") . "\n";
echo app::get('dev')->_(" Mysql读取数据正常...") . "\n";
} else {
echo app::get('dev')->_(" Mysql插入数据不正常...") . "\n";
//.........这里部分代码省略.........
示例15: disconnect
/**
* Disconnects from the server
*
* @return boolean
*/
static function disconnect()
{
if (!c::get('db.disconnect')) {
return false;
}
$connection = self::connection();
if (!$connection) {
return false;
}
// kill the connection
$disconnect = @mysql_close($connection);
self::$connection = false;
if (!$disconnect) {
return self::error(l::get('db.errors.disconnect', 'Disconnecting database failed'));
}
return true;
}