connect() public method

连接数据库方法
public connect ( $config = '', $linkNum )
 function __construct()
 {
     include 'files/config.php';
     $msql = new Db();
     $msql->connect();
     $this->is_login();
 }
Example #2
0
 public function dologinAction()
 {
     Db::connect();
     $bean = R::dispense('user');
     // the redbean model
     $required = ['Name' => 'name', 'Email' => 'email', 'User_Name' => ['rmnl', 'az_lower'], 'Password' => 'password_hash'];
     \RedBeanFVM\RedBeanFVM::registerAutoloader();
     // for future use
     $fvm = \RedBeanFVM\RedBeanFVM::getInstance();
     $fvm->generate_model($bean, $required);
     //the magic
     R::store($bean);
     $val = new validation();
     $val->addSource($_POST)->addRule('email', 'email', true, 1, 255, true)->addRule('password', 'string', true, 10, 150, false);
     $val->run();
     if (count($val->errors)) {
         Debug::r($val->errors);
         foreach ($val->errors as $error) {
             Notification::setMessage($error, Notification::TYPE_ERROR);
         }
         $this->redirect(Request::createUrl('login', 'login'));
     } else {
         Notification::setMessage("Welcome back !", Notification::TYPE_SUCCESS);
         Debug::r($val->sanitized);
         session::set('user', ['sanil']);
         $this->redirect(Request::createUrl('index', 'index'));
     }
 }
Example #3
0
function conectar()
{
    $con = new Db("localhost", "root", "benjamin13");
    $con->connect();
    $con->select_db("conquistadores");
    return $con;
}
Example #4
0
 public function save()
 {
     $query = "INSERT INTO miembros values (null,'" . $this->getNombre() . "',null,'" . $this->getEmail() . "')";
     $conn = new Db("localhost", "root", "benjamin13");
     $conn->connect();
     $conn->insert($query, "conquistadores");
 }
 public function login()
 {
     $conn = Db::connect();
     $sql = 'SELECT user_id FROM users ' . ' WHERE email = ? AND password = ?';
     $email = $conn->real_escape_string($_POST['email']);
     $password = md5($_POST['password']);
     $statement = $conn->prepare($sql);
     $statement->bind_param('ss', $email, $password);
     $statement->execute();
     $statement->bind_result($userId);
     $statement->fetch();
     if ($userId == NULL) {
         return false;
     } else {
         session_start();
         $_SESSION['user_id'] = $userId;
         if (isset($_SESSION['cart']) && $_SESSION['cart'] != []) {
             require_once '../app/models/Cart.php';
             $cart = new Cart();
             foreach ($_SESSION['cart'] as $product_id => $value) {
                 $cart->addProductToCart($product_id, $_SESSION['cart'][$product_id]['cart_quantity']);
             }
         }
         return true;
     }
 }
Example #6
0
 public static function getStandartTheme()
 {
     $db = Db::connect();
     $query = "SELECT themes.file FROM themes WHERE themes.id = 1";
     $result = $db->query($query)->fetch(PDO::FETCH_ASSOC);
     return $result['file'];
 }
Example #7
0
 public function deleteArticleById($id)
 {
     $pdo = Db::connect();
     $sql = "DELETE FROM `article` WHERE `article_id` = {$id}";
     $query_result = $pdo->query($sql);
     return $query_result;
 }
Example #8
0
 /**
  * Prywatny konstruktor klasy inicjujący połączenie z bazą danych
  * @global type $CONFIG mixed tablica przechowująca configuracje systemu
  */
 private function __construct()
 {
     global $CONFIG;
     self::$connect = new PDO('mysql:host=' . $CONFIG["host"] . ";dbname=" . $CONFIG["database"], $CONFIG["user"], $CONFIG["pass"]);
     self::$connect->query("SET NAMES utf8");
     self::$connect->query("SET CHARSET utf8");
     self::$connect->num_queries = 0;
 }
Example #9
0
 /**
  * Return an instance of Db Class
  * If not connection to the database is made then firstly will try to connect to the database
  * If connection to the database fails then `false` value will be returned
  * 
  * @return  Mixed;
  */
 public function db()
 {
     if (!self::$db) {
         self::$db = new Db($this->config('db'));
         self::$db->connect();
     }
     return self::$db;
 }
 public static function incrementProductQuantity($productId, $quantity)
 {
     $sql = "UPDATE shopping_cart SET cart_quantity = cart_quantity + ? " . "WHERE fk_shopping_cart_user = ? AND fk_shopping_cart_product_version = ?";
     $conn = Db::connect();
     $statement = $conn->prepare($sql);
     $statement->bind_param('iii', $quantity, $_SESSION['user_id'], $productId);
     $statement->execute();
 }
 public function setup()
 {
     global $baseParams;
     $db = new Db($baseParams['__YKVAL_DB_DSN__'], 'root', 'lab', $baseParams['__YKVAL_DB_OPTIONS__']);
     $db->connect();
     # $db->truncateTable('queue');
     $db->disconnect();
 }
Example #12
0
 public function destroy($wishListId)
 {
     $sql = 'DELETE FROM wish_list WHERE wish_list_id = ?';
     $conn = Db::connect();
     $stmt = $conn->prepare($sql);
     $stmt->bind_param('i', $wishListId);
     $stmt->execute();
     $stmt->close();
 }
Example #13
0
 static function table($with_db_name = true)
 {
     $conn = Db::getConnection('streams');
     $prefix = empty($conn['prefix']) ? '' : $conn['prefix'];
     $table_name = $prefix . 'notification';
     if (!$with_db_name) {
         return $table_name;
     }
     $db = Db::connect('streams');
     return $db->dbName() . '.' . $table_name;
 }
Example #14
0
 public function emptyCart()
 {
     $sql = 'DELETE FROM shopping_cart WHERE fk_shopping_cart_user = ?';
     $database = Db::connect();
     $statement = $database->prepare($sql);
     $statement->bind_param('i', $_SESSION['user_id']);
     $statement->execute();
     if (isset($_SESSION['cart'])) {
         unset($_SESSION['cart']);
     }
 }
 private function runConfirmationQuery($sql, $id)
 {
     $conn = Db::connect();
     $stmt = $conn->prepare($sql);
     $stmt->bind_param('ii', $id, $_SESSION['user_id']);
     $stmt->execute();
     $stmt->bind_result($id);
     $stmt->fetch();
     $conn->close();
     $_SESSION['test'] = $id;
     return $id;
 }
Example #16
0
 private static function sql_inner()
 {
     $db = Db::connect();
     $fna = func_num_args();
     $result = $db->prepare(func_get_arg(0));
     $result->bindParam(':login', func_get_arg(1), PDO::PARAM_INT);
     if (func_num_args() > 2) {
         $result->bindParam(':password', func_get_arg(2), PDO::PARAM_INT);
     }
     $result->execute();
     return $result;
 }
Example #17
0
 public static function changeStandartTheme($newTheme)
 {
     $db = Db::connect();
     $query = "SELECT * FROM themes WHERE themes.id = :id";
     $stmt = $db->prepare($query);
     $stmt->bindValue(':id', $newTheme);
     $stmt->execute();
     $theme = $stmt->fetch(PDO::FETCH_ASSOC);
     $query = "UPDATE themes SET themes.name = :name, themes.file = :file WHERE themes.id = 1";
     $stmt = $db->prepare($query);
     $stmt->bindValue(':name', $theme['name']);
     $stmt->bindValue(':file', $theme['file']);
     $stmt->execute();
 }
Example #18
0
 public static function saveMessage($chatId, $userId, $message)
 {
     $message = Validator::clear($message);
     try {
         $db = Db::connect();
         $query = "INSERT INTO messages(text, chat_id, user_id, date) VALUES (:text, :chat_id, :user_id, NOW())";
         $stmt = $db->prepare($query);
         $stmt->bindParam(':text', $message);
         $stmt->bindParam(':chat_id', $chat_id);
         $stmt->bindParam(':user_id', $user_id);
         $stmt->execute();
     } catch (PDOException $e) {
         echo $e->getMessage();
     }
 }
Example #19
0
 public function findBy($columns, $val)
 {
     $conn = Db::connect();
     $sql = "SELECT  user_id, first_name, last_name, email, admin FROM users WHERE ";
     if (is_array($columns)) {
         foreach ($columns as $col) {
             $sql .= $col . "='" . $val[array_search($col, $col_list)] . "' AND  ";
         }
         $sql = rtrim($sql, " AND ");
     } else {
         $sql .= $columns . "='" . $val . "'";
     }
     $results = $conn->query($sql);
     return $results->fetch_assoc();
 }
Example #20
0
 public function __construct()
 {
     self::$root = rtrim(dirname($_SERVER['SCRIPT_FILENAME']), DIRECTORY_SEPARATOR);
     self::$base = rtrim(dirname($_SERVER['SCRIPT_NAME']), DIRECTORY_SEPARATOR);
     spl_autoload_register(array('TwCensus', 'loader'));
     Session::init();
     Session::$prefix = 'TwCensus_';
     Db::connect(self::$config['db']);
     Request::init();
     if (Request::hasQuery('kml')) {
         $this->actionKML();
     } else {
         $this->actionWebsite();
     }
 }
Example #21
0
 /**
  * Retrieve the table name to use in SQL statement
  * @method table
  * @static
  * @param {boolean} [$with_db_name=true] Indicates wheather table name should contain the database name
  * @return {string|Db_Expression} The table name as string optionally without database name if no table sharding
  * was started or Db_Expression class with prefix and database name templates is table was sharded
  */
 static function table($with_db_name = true)
 {
     if (Q_Config::get('Db', 'connections', 'Metrics', 'indexes', 'Session', false)) {
         return new Db_Expression(($with_db_name ? '{$dbname}.' : '') . '{$prefix}' . 'session');
     } else {
         $conn = Db::getConnection('Metrics');
         $prefix = empty($conn['prefix']) ? '' : $conn['prefix'];
         $table_name = $prefix . 'session';
         if (!$with_db_name) {
             return $table_name;
         }
         $db = Db::connect('Metrics');
         return $db->dbName() . '.' . $table_name;
     }
 }
Example #22
0
 public static function getInstance($dotype = "read")
 {
     if (!in_array($dotype, array("read", "write"))) {
         $dotype = "read";
     }
     $conf = c("db." . $dotype);
     if ($dotype == "write") {
         $instance =& self::$instance_master;
     } else {
         $instance =& self::$instance_slave;
     }
     if ($instance === NULL || !$instance instanceof Db) {
         $instance = new Db();
         $instance->connect($conf);
     }
 }
 public static function addQuiz($postId, $question, $answers)
 {
     $db = Db::connect();
     $query = "INSERT INTO quizes(question, post_id) VALUES(:question, :post_id)";
     $stmt = $db->prepare($query);
     $stmt->bindParam(':question', $question);
     $stmt->bindParam(':post_id', $postId);
     $stmt->execute();
     $query = "SELECT MAX(quizes.id) AS id FROM quizes";
     $result = $db->query($query)->fetch(PDO::FETCH_ASSOC);
     $id = $result['id'];
     $query = "INSERT INTO quiz_answers(quiz_id, answer) VALUES(:id, :answer)";
     $stmt = $db->prepare($query);
     foreach ($answers as $answer) {
         $stmt->bindParam(':answer', $answer);
         $stmt->bindParam(':id', $id);
         $stmt->execute();
     }
 }
 /**
  * test_ExecAndTransaction_ExecWithInTrans()
  *
  * exec()のテスト(トランザクションあり)
  */
 public function test_ExecAndTransaction_ExecWithInTrans()
 {
     $params = ["driver" => $GLOBALS['DB_DRIVER'], "user" => $GLOBALS['DB_USER'], "pass" => $GLOBALS['DB_PASSWORD'], "dbname" => $GLOBALS['DB_DBNAME'], "host" => $GLOBALS['DB_HOST'], "persistent" => false];
     $instance = new Db();
     $instance->connect($params);
     // commit pattern
     $this->assertTrue($instance->beginTransaction());
     $this->assertTrue($instance->inTransaction());
     $this->assertEquals(1, $instance->exec('INSERT INTO risoluto_db_test(id, column1, column2) values ("10", "TEST_A", "TEST_B");'));
     $this->assertEquals(2, $this->getconnection()->getRowCount('risoluto_db_test'));
     $this->assertEquals(10, $instance->lastInsertId());
     $this->assertEquals(10, $instance->lastInsertId('id'));
     $this->assertTrue($instance->commit());
     $this->assertFalse($instance->inTransaction());
     $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test'));
     // Rollback pattern
     $before_val = $this->getconnection()->createQueryTable('risoluto_db_test', 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";');
     $this->assertTrue($instance->beginTransaction());
     $this->assertTrue($instance->inTransaction());
     $this->assertEquals(1, $instance->exec('UPDATE risoluto_db_test SET column1="TEST_C", column2="TEST_C" WHERE id="10";'));
     $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test'));
     $this->assertEquals(1, $instance->exec('DELETE FROM risoluto_db_test WHERE id="10";'));
     $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test'));
     $this->assertTrue($instance->rollBack());
     $this->assertFalse($instance->inTransaction());
     $after_val = $this->getconnection()->createQueryTable('risoluto_db_test', 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";');
     $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test'));
     $this->assertTablesEqual($before_val, $after_val);
     // Cleaning
     $this->assertEquals(1, $instance->exec('DELETE FROM risoluto_db_test WHERE id="10";'));
     $this->assertEquals(2, $this->getconnection()->getRowCount('risoluto_db_test'));
     $instance->disConnect();
     unset($instance);
 }
Example #25
0
 /**
  * 实例化数据库
  * @param mixed $config 数据库配置
  * @return object
  */
 public static function db($config = [])
 {
     return Db::connect($config);
 }
Example #26
0
 /** @return Db_Mysql */
 static function db()
 {
     return Db::connect('streams');
 }
Example #27
0
 /**
  * This method calls Db.connect() using information stored in the configuration.
  * If this has already been called, then the same db object is returned.
  * @method db
  * @return {iDb} The database object
  */
 static function db()
 {
     return Db::connect('Platform');
 }
Example #28
0
<?php

// DBライブラリ読み込み
require 'lib/db.php';
// 格納する引数受け取り
$title = isset($_POST['title']) ? $_POST['title'] : NULL;
$detail = isset($_POST['detail']) ? $_POST['detail'] : NULL;
$asin = isset($_POST['asin']) ? $_POST['asin'] : NULL;
$medium_image_url = isset($_POST['medium_image_url']) ? $_POST['medium_image_url'] : NULL;
$large_image_url = isset($_POST['large_image_url']) ? $_POST['large_image_url'] : NULL;
// MySQL接続
$db = new Db();
$pdo = $db->connect();
if (!$pdo) {
    exit(1);
}
// UPDATE
$sqls = array("UPDATE item SET\n        title = '{$title}',\n        detail = '{$detail}',\n        medium_image_url = '{$medium_image_url}',\n        large_image_url = '{$large_image_url}',\n        create_time = NOW()\n    WHERE asin = '{$asin}'", "UPDATE other SET\n        title = '{$title}',\n        detail = '{$detail}',\n        medium_image_url = '{$medium_image_url}',\n        large_image_url = '{$large_image_url}',\n        create_time = NOW()\n    WHERE asin = '{$asin}'");
$status = array();
foreach ($sqls as $sql) {
    $stmt = $pdo->prepare($sql);
    if (!$stmt) {
        error_log(var_export($pdo->errorInfo(), true));
        $status[] = false;
    }
    if (!$stmt->execute()) {
        $status[] = false;
    } else {
        $status[] = true;
    }
}
Example #29
0
 /**
  * This method calls Db.connect() using information stored in the configuration.
  * If this has already been called, then the same db object is returned.
  * @method db
  * @return {iDb} The database object
  */
 static function db()
 {
     return Db::connect('Websites');
 }
Example #30
0
 /**
  * 切换当前的数据库连接
  * @access public
  * @param mixed $linkId  连接标识
  * @param mixed $config  数据库连接信息
  * @return Model
  */
 public function db($linkId = '', $config = '')
 {
     if ('' === $linkId && $this->db) {
         return $this->db;
     }
     if (!isset($this->links[$linkId])) {
         // 创建一个新的实例
         if (is_string($linkId) && '' == $config) {
             $config = Config::get($linkId);
         }
         $this->links[$linkId] = Db::connect($config);
     } elseif (null === $config) {
         $this->links[$linkId]->close();
         // 关闭数据库连接
         unset($this->links[$linkId]);
         return;
     }
     // 切换数据库连接
     $this->db = $this->links[$linkId];
     $this->_after_db();
     return $this;
 }