con = mysql_connect($server, $user, $password);
if(!$this->con){
throw new MySQLConnectException("データベースの接続に失敗しました。");
}
$this->selectdb = mysql_select_db($db, $this->con);
if(!$this->selectdb){
throw new MySQLDatabaseSelectException("データベースの選択に失敗しました。");
}
mysql_set_charset(convertCharset($charset), $this->con);
return $this->con;
}
// 文字コードの取得
function convertCharset($charset){
$charsetArray = array(
"UTF-8" => "utf8",
"Shift_JIS" => "sjis",
"EUC-JP" => "usjis"
);
return $charsetArray[$charset];
}
final function __destruct(){
// mysql_close($this->con);
}
}
class SqlCommand {
var $sql;
var $con;
private $pattern;
private $replacement;
private $result;
private $insertSql, $insertResult, $insertArray;
// コンストラクタ
function __construct($con, $sql){
$this->sql = $sql;
$this->con = $con;
}
function query() {
$this->result = mysql_query($this->sql,$this->con);
if(!$this->result) {
$this->result = mysql_query($this->sql, $this->con);
if(DEBUG_MODE === 1) var_dump($this->getErrors());
throw new MySQLExecuteException("クエリの実行に失敗しました。");
}
return $this->result;
}
// 変更された行の数を取得
function getAffectedRows() {
return mysql_affected_rows();
}
// 挿入IDの取得
function getInsertId() {
$this->insertSql = "SELECT LAST_INSERT_ID() AS insert_id;";
$this->insertResult = mysql_query($this->insertSql, $this->con);
$this->insertArray = mysql_fetch_array($this->insertResult);
return $this->insertArray['insert_id'];
}
// 検索結果の取得
function getArray() {
return mysql_fetch_assoc($this->result);
}
// 検索結果をすべて取得
function getAllArray() {
for($this->i=0; $this->array[$this->i++] = mysql_fetch_assoc($this->result); );
// var_dump($this->array);
array_pop($this->array);
return $this->array;
}
// 列数の取得
function getCols() {
return mysql_num_fields($this->result);
}
// 行数の取得
function getRows() {
return mysql_num_rows($this->result);
}
// エラー
function getErrors() {
return mysql_errno() .": " . mysql_error();
}
// エラーナンバー
function getErrorNo() {
return mysql_errno();
}
// パラメータの追加
function addWithValue($parameter, $value) {
// null判定
if( $value === null) {
// パラメータの置き換え
$this->pattern = "/({1}\s*|=\s*)(" . $parameter . ")(\s*,?)/";
$this->replacement = '${1}'. "null" . '${3^';
$this->sql = preg_replace($this->pattern, $this->replacement, $this->sql);
}
// 数値判定
elseif(is_numeric($value)){
// パラメータの置き換え
$this->pattern = "/({1}\s*|=\s*)(" . $parameter . ")(\s*,?)/";
$this->replacement = '${1}'. $value . '${3}';
$this->sql = preg_replace($this->pattern, $this->replacement, $this->sql);
}
// 文字列判定
elseif(is_string($value)) {
// print "String";
// サニタイジング
if(get_magic_quotes_gpc() == 1) {
$value = stripshashes($value);
$value = mysql_real_escape_string($value);
}else {
$value = mysql_real_escape_string($value);
}
// パラメータの置き換え
$this->pattern = "/({1}\s*|={1}\s*|like{1}\s*)(%*)" . $parameter . "(%*)(\s,?)/i";
$this->replacement = '${1}"${2}'. $value . '${3}"{4}';
$this->sql = preg_replace($this->pattern, $this->replacement, $this->sql);
}
// その他は例外を投げる
else {
throw new IllegalStateExeption("処理できない値が渡されました");
}
}
}
class MySQLConnectException extends Exception {}
class MySQLDatabaseSelectException extends Exception {}
class IllegalStateException extends Exception {}
class MySQLExecuteException extends Exception {}
?>
con = mysqli_connect($server, $user, $password);
if(!$this->con){
throw new MySQLConnectException("データベースの接続に失敗しました。");
}
$this->selectdb = mysqli_select_db($db, $this->con);
if(!$this->selectdb){
throw new MySQLDatabaseSelectException("データベースの選択に失敗しました。");
}
mysqli_set_charset(convertCharset($charset), $this->con);
return $this->con;
}
// 文字コードの取得
function convertCharset($charset){
$charsetArray = array(
"UTF-8" => "utf8",
"Shift_JIS" => "sjis",
"EUC-JP" => "usjis"
);
return $charsetArray[$charset];
}
final function __destruct(){
// mysql_close($this->con);
}
}
class SqlCommand {
var $sql;
var $con;
private $pattern;
private $replacement;
private $result;
private $insertSql, $insertResult, $insertArray;
// コンストラクタ
function __construct($con, $sql){
$this->sql = $sql;
$this->con = $con;
}
function query() {
$this->result = mysqli_query($this->sql,$this->con);
if(!$this->result) {
$this->result = mysqli_query($this->sql, $this->con);
if(DEBUG_MODE === 1) var_dump($this->getErrors());
throw new MySQLExecuteException("クエリの実行に失敗しました。");
}
return $this->result;
}
// 変更された行の数を取得
function getAffectedRows() {
return mysqli_affected_rows();
}
// 挿入IDの取得
function getInsertId() {
$this->insertSql = "SELECT LAST_INSERT_ID() AS insert_id;";
$this->insertResult = mysqli_query($this->insertSql, $this->con);
$this->insertArray = mysqli_fetch_array($this->insertResult);
return $this->insertArray['insert_id'];
}
// 検索結果の取得
function getArray() {
return mysqli_fetch_assoc($this->result);
}
// 検索結果をすべて取得
function getAllArray() {
for($this->i=0; $this->array[$this->i++] = mysqli_fetch_assoc($this->result); );
// var_dump($this->array);
array_pop($this->array);
return $this->array;
}
// 列数の取得
function getCols() {
return mysqli_num_fields($this->result);
}
// 行数の取得
function getRows() {
return mysqli_num_rows($this->result);
}
// エラー
function getErrors() {
return mysqli_errno() .": " . mysql_error();
}
// エラーナンバー
function getErrorNo() {
return mysqli_errno();
}
// パラメータの追加
function addWithValue($parameter, $value) {
// null判定
if( $value === null) {
// パラメータの置き換え
$this->pattern = "/({1}\s*|=\s*)(" . $parameter . ")(\s*,?)/";
$this->replacement = '${1}'. "null" . '${3^';
$this->sql = preg_replace($this->pattern, $this->replacement, $this->sql);
}
// 数値判定
elseif(is_numeric($value)){
// パラメータの置き換え
$this->pattern = "/({1}\s*|=\s*)(" . $parameter . ")(\s*,?)/";
$this->replacement = '${1}'. $value . '${3}';
$this->sql = preg_replace($this->pattern, $this->replacement, $this->sql);
}
// 文字列判定
elseif(is_string($value)) {
// print "String";
// サニタイジング
if(get_magic_quotes_gpc() == 1) {
$value = stripshashes($value);
$value = mysqli_real_escape_string($value);
}else {
$value = mysqli_real_escape_string($value);
}
// パラメータの置き換え
$this->pattern = "/({1}\s*|={1}\s*|like{1}\s*)(%*)" . $parameter . "(%*)(\s,?)/i";
$this->replacement = '${1}"${2}'. $value . '${3}"{4}';
$this->sql = preg_replace($this->pattern, $this->replacement, $this->sql);
}
// その他は例外を投げる
else {
throw new IllegalStateExeption("処理できない値が渡されました");
}
}
}
class MySQLConnectException extends Exception {}
class MySQLDatabaseSelectException extends Exception {}
class IllegalStateException extends Exception {}
class MySQLExecuteException extends Exception {}
?>
MySQLに文字コードを設定するためのmysql_set_charset に変更しています。
mysql_*の関数がPHP7.0以降で削除されたので「mysql」の部分を mysqliに変えただけの同じプログラムを用意しました。