Skip to content

Instantly share code, notes, and snippets.

@batako
Created September 20, 2012 13:59
Show Gist options
  • Save batako/3756095 to your computer and use it in GitHub Desktop.
Save batako/3756095 to your computer and use it in GitHub Desktop.
PHP : MySQLへの接続
class db{
// ホスト名
protected $host = null;
// ユーザ名
protected $id = null;
// パスワード
protected $pw = null;
// データベース名
protected $db = null;
// セッション
protected $link = array();
// クエリー
protected $query = null;
// クエリーの実行結果
protected $result = null;
#--------------------------------------------------#
# 初期設定
#--------------------------------------------------#
/**
*
* @param $path_init_file : 初期設定ファイル
*
*/
function __construct($path_init_file = null){
// 設定ファイルの読み込み
$this->load_init_file($path_init_file);
}
#--------------------------------------------------#
# 設定ファイルの読み込み
#--------------------------------------------------#
/**
*
* @param $path_init_file : 設定ファイルパス
*
*/
function load_init_file($path_init_file){
// 読み込み
include($path_init_file);
// ホスト名
$this->set_host($data['host']);
// ユーザ名
$this->set_id($data['id']);
// パスワード
$this->set_pw($data['pw']);
// データベース名
$this->set_db($data['db']);
}
#--------------------------------------------------#
# ホストの設定
#--------------------------------------------------#
/**
*
* @param $host : ホスト名
*
*/
function set_host($host){
$this->host = $host;
}
#--------------------------------------------------#
# ユーザー名の設定
#--------------------------------------------------#
/**
*
* @param $id : ユーザ名
*
*/
function set_id($id){
$this->id = $id;
}
#--------------------------------------------------#
# パスワードの設定
#--------------------------------------------------#
/**
*
* @param $pw : パスワード
*
*/
function set_pw($pw){
$this->pw = $pw;
}
#--------------------------------------------------#
# データベースの設定
#--------------------------------------------------#
/**
*
* @param $db : データベース名
*
*/
function set_db($db){
$this->db = $db;
}
#--------------------------------------------------#
# データベースへの接続
#--------------------------------------------------#
function connect(){
// サーバー接続
$link = $this->connect_server();
// データベース接続
$db = $this->connect_database();
// 接続成功
if($link && $db){
// セッション保持
array_push($this->link, $link);
// オートコミットOFF
//mysql_query('SET AUTOCOMMIT=0', $link);
//トランザクション開始
//mysql_query('BEGIN', $link);
// 文字コード設定
$this->character_set();
return true;
}
// 接続失敗
else return false;
}
#--------------------------------------------------#
# サーバー接続
#--------------------------------------------------#
function connect_server(){
return mysql_connect($this->host, $this->id, $this->pw);
}
#--------------------------------------------------#
# データベース接続
#--------------------------------------------------#
function connect_database(){
return mysql_select_db($this->db);
}
#--------------------------------------------------#
# 文字コードの設定
#--------------------------------------------------#
/**
*
* @param $character_code : 文字コード
*
*/
function character_set($character_code = 'utf8'){
// MySQLのクライアントの文字コードをutf8に設定
mysql_query("SET NAMES {$character_code}");
}
#--------------------------------------------------#
# Queryの設定
#--------------------------------------------------#
/**
*
* @param $query_format : クエリーのフォーマット
* @param $query_args : クエリーの引数
*
*/
function set_query($query_format, $query_args){
// SQL 文中で用いる文字列の特殊文字をエスケープする
$conver_query_args = array();
foreach ($query_args as $value){
array_push($conver_query_args, $this->quote_smart($value));
}
// Query生成
$this->query = vsprintf($query_format, $conver_query_args);
}
#--------------------------------------------------#
# 特殊文字をエスケープ
#--------------------------------------------------#
/**
*
* @param $str : 変換文字
*
*/
function quote_smart($str){
// magic_quotesが有効ならクォートされた文字列のクウォート部分を除去
if(get_magic_quotes_gpc()){
$str = stripslashes($str);
}
// 数値あるいは数値形式の文字列以外をクウォート
if(!is_numeric($str)){
$str = "'".mysql_real_escape_string($str)."'";
}
return $str;
}
#--------------------------------------------------#
# クエリーの実行
#--------------------------------------------------#
/**
*
* @param $link : セッション
*
*/
function run($link = null){
// セッションの指定がなければセッション配列の最後のセッションで実行
//if(!$link) $link = $this->link[count($this->link) - 1];
$this->result = mysql_db_query($this->db, $this->query);
//$this->result = mysql_query($this->query, $link);
if($this->result){
return true;
}
else{
return false;
}
}
#--------------------------------------------------#
# コミット
#--------------------------------------------------#
/**
*
* @param $link : セッション
*
*/
function commit($link = null){
if($link){
mysql_query('COMMIT', $link);
}else{
mysql_query('COMMIT');
}
}
#--------------------------------------------------#
# ロールバック
#--------------------------------------------------#
/**
*
* @param $link : セッション
*
*/
function rollback($link = null){
if($link){
mysql_query('ROLLBACK', $link);
}else{
mysql_query('ROLLBACK');
}
}
#--------------------------------------------------#
# クエリーの実行結果をコンバート
#--------------------------------------------------#
function convert_resutl(){
// 実行結果を1行ずつ取得しデータを格納する
$MyRow = array();
while($row=mysql_fetch_array($this->result, MYSQL_ASSOC)){
array_push($MyRow, $row);
}
return $MyRow;
}
#--------------------------------------------------#
# データベースの切断
#--------------------------------------------------#
function close(){
//結果レコードをメモリから開放
mysql_free_result($this->result);
//DBへの接続を切断
foreach ($this->link as $link){
mysql_close($link);
}
}
}
@batako
Copy link
Author

batako commented Sep 20, 2012

トランザクション処理無効

@batako
Copy link
Author

batako commented Sep 20, 2012

トランザクション処理無効

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment