Ejemplo n.º 1
0
 private static function _connect()
 {
     self::$_con = mysql_connect(self::$host, self::$user, self::$pswd);
     if (!self::$_con) {
         return false;
     }
     if (!mysql_query('CREATE DATABASE IF NOT EXISTS ' . self::$db, self::$_con)) {
         return false;
     }
     if (!mysql_query("set names'UTF8'")) {
         return false;
     }
     return mysql_select_db(self::$db, self::$_con);
 }
Ejemplo n.º 2
0
 private function _init_db()
 {
     $sql = 'CREATE TABLE IF NOT EXISTS ' . $this->tablename . ' ( ';
     $sql .= '`id` bigint(20) NOT NULL AUTO_INCREMENT';
     foreach ($this->fields as $name => $v) {
         if (!isset($v['type'])) {
             //neccessary
             continue;
         }
         $fieldstr = ', ';
         $fieldstr .= sprintf("`%s` %s", $name, $v['type']);
         if (isset($v['len'])) {
             $fieldstr .= sprintf("(%d)", intval($v['len']));
         }
         if (isset($v['notnull']) && $v['notnull'] === true) {
             $fieldstr .= ' NOT NULL ';
         }
         if (isset($v['default'])) {
             if (is_numeric($v['default'])) {
                 $fieldstr .= sprintf(' DEFAULT %d ', $v['default']);
             } else {
                 $fieldstr .= sprintf(" DEFAULT '%s' ", $v['default']);
             }
         }
         $sql .= $fieldstr;
     }
     $sql .= ', PRIMARY KEY (`id`)';
     foreach ($this->indexs as $name => $v) {
         if (count($v) <= 0) {
             //neccessary
             continue;
         }
         $indexstr = sprintf(', KEY `%s` (', $name);
         foreach ($v as $field) {
             $indexstr .= sprintf(' `%s`,', $field);
         }
         $indexstr = substr($indexstr, 0, -1);
         //sub last ','
         $indexstr .= ')';
         $sql .= $indexstr;
     }
     $sql .= ') ENGINE=InnoDB DEFAULT CHARSET=utf8 ';
     return Common_Db_Base::query($sql);
 }