Permalink
Browse files

修正mysqli的bug

  • Loading branch information...
1 parent 45bcd7c commit f7f449ec3d03205d93555e6a9197fa3bc7f2b960 @leizongmin committed Mar 9, 2015
@@ -16,6 +16,7 @@
define('CONF_MYSQL_USER', 'root'); // 用户名
define('CONF_MYSQL_PASSWD', ''); // 密码
define('CONF_MYSQL_DBNAME', 'test'); // 数据库名
+define('CONF_MYSQL_CHARSET', 'utf8'); // 数据库编码
// 载入leiphp并初始化
File renamed without changes.
@@ -0,0 +1 @@
+<h1>Hello, world</h1>
@@ -17,8 +17,11 @@
function method_get () {
echo 'GET请求';
APP::dump($_GET);
+ APP::dump(SQL::getAll('show tables'));
+ APP::dump(SQL::getAll('show databases'));
+ APP::dump(SQL::getAll('select 1+1 as a'));
// 渲染模板
- APP::template();
+ APP::template('test.html');
}
// 处理POST请求
View
@@ -118,11 +118,16 @@ class SQL {
public static function connect ($server = 'localhost:3306', $username = 'root', $password = '', $database = '') {
$timestamp = microtime(true);
- SQL::$connection = mysqli_connect($server, $username, $password);
+ SQL::$connection = mysqli_connect($server, $username, $password, $database);
- $r = mysqli_select_db(SQL::$connection, $database);
- DEBUG::put('Connected: '.$username.'@'.$server.' permanent='.$permanent.' spent: '.round((microtime(true) - $timestamp) * 1000, 3).'ms', 'MySQL');
- return $r;
+ 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;
}
/**
@@ -163,7 +168,10 @@ public static function errmsg () {
* @return {String}
*/
public static function charset ($encoding = '') {
- if (!empty($encoding)) mysqli_set_charset(SQL::$connection, $encoding);
+ if (!empty($encoding)) {
+ mysqli_set_charset(SQL::$connection, $encoding);
+ DEBUG::put('charset='.$encoding, 'MySQL');
+ }
return mysqli_get_charset(SQL::$connection);
}
@@ -754,6 +762,10 @@ public static function render ($name, $locals = array(), $layout = '') {
}
}
+ public static function template ($name, $locals = array(), $layout = '') {
+ APP::render($name, $locals, $layout);
+ }
+
/**
* 设置模板变量
*
@@ -856,6 +868,7 @@ public static function init () {
$dbname = defined('CONF_MYSQL_DBNAME') ? CONF_MYSQL_DBNAME : '';
$permanent = defined('CONF_MYSQL_PERMANENT') ? CONF_MYSQL_PERMANENT : false;
SQL::connect($server, $user, $passwd, $dbname, $permanent);
+ if (defined('CONF_MYSQL_CHARSET')) SQL::charset(CONF_MYSQL_CHARSET);
}
// 自动执行 method_VERB

0 comments on commit f7f449e

Please sign in to comment.