<?php /** * MySQLを利用する場合のインターフェース * @author zhouz * @access public * @package jp.aimslib2.database.mysql */ class MySQLDatabaseAdaptor { public $host; public $port; public $db_name; public $user; public $pass; public $con = null; public $result; public $lastquery; /** * コンストラクタ */ function constructor(){ } /** * 初期化 */ function init($host, $port, $db_name, $user, $pass){ $this->host = $host; if(isset($port)){ $this->port = $port; }else{ $this->port = "3306"; } $this->db_name = $db_name; $this->user = $user; $this->pass = $pass; } /** * 接続の確立 */ public function db_connect(){ if (!is_resource($this->con)) { $this->con = @mysqli_connect($this->host, $this->user, $this->pass, "", $this->port, ""); // 文字コード変換指定が必要な場合 if (defined("MYSQL_USE_CLIENT_CHARSET") && MYSQL_USE_CLIENT_CHARSET != null) { if (function_exists('mysqli_set_charset') === false) { ErrorLogger::doOutput("using set names to:" . MYSQL_USE_CLIENT_CHARSET, 0, "MySQL"); // PHP4ではこっちを使う必要がある mysqli_query("SET NAMES " . MYSQL_USE_CLIENT_CHARSET, $this->con); } else { ErrorLogger::doOutput("using mysqli_set_charset to:" . MYSQL_USE_CLIENT_CHARSET, 0, "MySQL"); mysqli_set_charset(MYSQL_USE_CLIENT_CHARSET, $this->con); } } mysqli_select_db($this->con, $this->db_name); } } /** * 接続の解除(使わない) */ public function db_close() { mysqli_close($this->con); } /** * Queryを実行する。 * 結果は$resultにはいる。 */ public function db_exec($query) { $this->db_connect(); $this->lastquery = $query; ErrorLogger::doOutput($query, 0, "SQL"); //echo $query; if(@$this->result = mysqli_query($this->con, $query)){ $rowset = $this->private_getRowset($this->result); @mysqli_free_result($this->result); return $rowset; } else { ErrorLogger::doOutput(mysqli_error($this->con), 0); ErrorLogger::doOutput("error at:" . $this->lastquery, 0); } $this->db_close(); return "QUERY_ERROR"; } /** * 結果を2次元連想配列にして返します。 * @access private * @param resource $result 検索の結果リソース * @return array カラム名をkeyとした連想配列を要素として持つ配列 */ public function private_getRowset(&$result){ $rowset = array(); if (empty($result)) { return null; } //数值类型 if (is_numeric($result) || ($result===true) || ($result===false)) { return null; } $string_field = array(); $bool_field = array(); $fields = mysqli_fetch_fields($result); for ($x = 0; $x < count($fields); $x++ ) { $field = $fields[$x]; $name = $field->name; //ErrorLogger::doOutput("MYSQL TYPE:" . $name . " - " . $field->type . " - " . $field->length, 0); if ($field->type == 1) { // boolean array_push($bool_field, $name); } else { array_push($string_field, $name); } } while ($row = mysqli_fetch_assoc($result)) { foreach ($string_field as $name) { $row[$name] = SjisUtil::stripMysqlSlashesLike5C($row[$name]); } foreach ($bool_field as $name) { if ($row[$name] == 1) { $row[$name] = true; } else { $row[$name] = false; } } array_push($rowset, $row); } return $rowset; } } ?>