public static function connect($server = 'localhost:3306', $username = '******', $password = '', $database = '') { //microtime(get_as_float) //参数 描述 //get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。 $timestamp = microtime(true); SQL::$connection = mysqli_connect($server, $username, $password, $database); //mysqli_connect(host,username,password,dbname,port,socket); //参数 描述 //host 可选。规定主机名或 IP 地址。 //username 可选。规定 MySQL 用户名。 //password 可选。规定 MySQL 密码。 //dbname 可选。规定默认使用的数据库。 //port 可选。规定尝试连接到 MySQL 服务器的端口号。 //socket 可选。规定 socket 或要使用的已命名 pipe。 //round(x,prec) //参数 描述 //x 可选。规定要舍入的数字。 //prec 可选。规定小数点后的位数。 //说明 //返回将 x 根据指定精度 prec (十进制小数点后数字的数目)进行四舍五入的结果。prec 也可以是负数或零(默认值)。 DEBUG::put('Connected: ' . $username . '@' . $server . ' spent: ' . round((microtime(true) - $timestamp) * 1000, 3) . 'ms', 'MySQL'); $err = SQL::error(); if ($err['id'] > 0) { DEBUG::put(' - Error: #' . $err['id'] . ' ' . $err['error'], 'MySQL'); } return SQL::$connection; }
/** * Gets a connection to the MySQL database. * @throws mysqli_sql_exception * @return mysqli */ public static function getConnection() { if (is_null(SQL::$connection)) { SQL::$connection = new mysqli(Config::HOST, Config::USERNAME, Config::$password, Config::DATABASE); SQL::$connection->set_charset('utf8'); } if (SQL::$connection->connect_error) { throw new mysqli_sql_exception("Failed to connect to database: " . SQL::$connection->connect_error); } return SQL::$connection; }
static function connect() { if (is_null(self::$connection)) { $sqlData = Config::getSQL(); $conn = new mysqli($sqlData["server"], $sqlData["name"], $sqlData["pass"], $sqlData["db"]); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } self::$connection = $conn; } return self::$connection; }
/** * 连接到数据库 * 成功返回true, 失败返回false * * @param string $server * @param string $username * @param string $password * @param string $database * @return bool */ public static function connect($server = 'localhost:3306', $username = '******', $password = '', $database = '') { $timestamp = microtime(true); SQL::$connection = mysqli_connect($server, $username, $password, $database); DEBUG::put('Connected: ' . $username . '@' . $server . ' spent: ' . round((microtime(true) - $timestamp) * 1000, 3) . 'ms', 'MySQL'); $err = SQL::error(); if ($err['id'] > 0) { DEBUG::put(' - Error: #' . $err['id'] . ' ' . $err['error'], 'MySQL'); } return SQL::$connection; }