|
|
| |
php5 mysqli µðºñ Á¢¼Ó Ŭ·¡½º ÇÁ·Î±×·¥ ¹× »ç¿ë¹ý |
|
|
|
8³â Àü |
À¥½ºÅ©¸³Æ®¾ð¾îÀÇ ÃÖ°ÀÚ PHP ÇÁ·Î±×·¥ÀÔ´Ï´Ù.
PHP¸¦ »ç¿ëÇÏ·Á¸é ¹Ýµå½Ã ¾Ë¾Æ¾ß ÇÒ°ÍÀÌ µðºñÁ¢¼ÓÀ» ÇÏ¿© ±× µ¥ÀÌŸ¸¦ ÃßÃâÇÒ ÁÙ ¾Ë¾Æ¾ß
ÁÁÀº ÇÁ·Î±×·¥À» ¸¸µé ¼ö ÀÖÁö¿ä
PHP5 ºÎÅÍ´Â ¸¹À½ Ŭ·¡½º µéÀ» Á¦°øÇϴµ¥
±× Áß¿¡ Çϳª°¡ mysqli ¶ó´Â Ŭ·¡½º ÀÔ´Ï´Ù.
ÀÌ Å¬·¡½º mysqli ¸¦ ±×´ë·Î »ó¼Ó¹Þ¾Æ
Á»´õ »ç¿ëÇϱ⠽±µµ·Ï mysqli °ü·Ã Ŭ·¡½º¸¦ ¸¸µéµå·Ï ÇÏ°Ú½À´Ï´Ù.
ÇÏÁö¸¸, ¸ðµç ¾ð¾î°¡ ±×·¸µíÀÌ
PHPµµ ¹Ýµå½Ã mysql ¿¡¸¸ Á¢¼Ó Çؼ »ç¿ëÇ϶ó´Â ¹ýÀÌ ¾ø½À´Ï´Ù.
´Ù¾çÇÑ µ¥ÀÌŸº£À̽º¿¡ Á¢¼ÓÇؼ »ç¿ëÀ» ÇÒ ¼ö ÀÖÁÒ
±×·¸Áö¸¸ php ÄÚµùÀº °®½À´Ï´Ù.
´Ü µðºñ¸¸ ¹Ù²î´Â °ÅÁÒ ÀÌ·¸°Ô µðºñ¸¸ ¹Ù²î¸é µÇ´Âµ¥
µðºñ¸¦ Á¢¼ÓÇÏ°í Äõ¸® ³¯¸®°í ÀúÀåÇÏ°í ÇÏ´Â Àϵ鿡 ´ëÇØ
ÇÁ·Î±×·¥À» ¼öÁ¤ÇØ¾ß ÇÑ´Ù¸é Á¤¸» ÀÏÀÌ ¸¹°í ½ºÆ®·¹½º°¡ »ó½ÂÇÏ°Ô µË´Ï´Ù.
ÀÌ·¯ÇÑ ºÒÆíÇÑ ÀÛ¾÷µéÀ» ÇÏÁö ¾Ê±â À§Çؼ
ÇϳªÀÇ interface¸¦ Á¤ÀÇÇØ ³õ°í
´Ù¸¥ ¿©·¯ ÇÁ·Î±×·¡¸ÓµéÀÌ µ¥ÀÌŸ º£À̽º ÇÁ·Î±×·¥À» ÀÛ¼º ÇÒ¶§
µ¿ÀÏÇÑ ¸Þ¼ÒµåµéÀ» Á¤ÀÇÇÏ°í »ç¿ëÇϵµ·Ï ±ÔÄ¢À» Á¤ÇÕ´Ï´Ù.
°¡Àå ¸¹ÀÌ »ç¿ëÇÏ´Â
query, insert, modify, delete µîÀÌÁÒ
ÃÖ¼ÒÇÑÀÇ °ÍÀ» Á¤ÀÇÇؼ »ç¿ëÇÏ´Â °Ì´Ï´Ù.
´õ ¸¹Àº °ÍµéÀ» Á¤ÀÇ ÇÒ ¼ö ÀÖÁö¸¸
¿ì¼± À§ÀÇ °ÍµéÀ» Á¤ÀÇÇÑ interface ¸¦ ¸¸µé°í mysqli¸¦ »ó¼Ó ¹Þ¾Æ
º¸´Ù »ç¿ëÇϱâ Æí¸®ÇÑ µ¥ÀÌŸº£À̽º Ŭ·¡½º ÆÄÀÏÀ» ¸¸µé¾î º¸°Ú½À´Ï´Ù.
@ interface : DbSwitch Ŭ·¡½º¸¦ ¸¸µé°Ú½À´Ï´Ù.
< ?php
# purpose : °¢Á¾ SQL °ü·Ã µðºñ¸¦ ÅëÀϼºÀÖ°Ô ÀÛ¼ºÇÒ ¼ö ÀÖµµ·Ï ƲÀ» Á¦°ø
interface DbSwitch
{
public function query($query); # Äõ¸®
public function insert($table); # ÀúÀå
public function update($table,$where); # ¼öÁ¤
public function delete($table,$where); # »èÁ¦
public function bindParams($query,$args=array()); #Äõ¸® ¹®ÀÚ ¹ÙÀεåÈ¿°ú
}
? >
@ µðºñ Á¢¼Ó Á¤º¸¸¦ ¹Ì¸® ¼³Á¤ÇØ ³õ°Ú½À´Ï´Ù.
< ?php
define('_DB_HOST_','localhost'); #Á¢¼Ó °æ·Î
define('_DB_USER_','apmsoft'); #Á¢¼Ó ¾ÆÀ̵ð
define('_DB_PASSWD_','password'); #Á¢¼Ó ºñ¹Ð¹øÈ£
define('_DB_NAME_','apmsoft_db'); #Á¢¼Ó µ¥ÀÌŸº£À̽º ¸í
? >
@ DbMySqli : [Ŭ·¡½º1°³] mysqli, [ÀÎÅÍÆäÀ̼ 2°³] DbSwitch, ArrayAccess ¸¦ »ó¼Ó ¹Þ¾Æ ±¸ÇöÀ» ÇÕ´Ï´Ù.
< ?php
# Parent : MySqli
# Parent : DBSwitch
# purpose : mysqliÀ» È°¿ëÇØ È®ÀåÇÑ´Ù
class DbMySqli extends mysqli implements DbSwitch,ArrayAccess
{
private $params = array();
# dsn : host:dbname = localhost:dbname
public function __construct($dsn='',$user='',$passwd='',$chrset='utf8')
{
# µ¥ÀÌŸº£À̽º Á¢¼Ó
if(!empty($dsn)){
$dsn_args = explode(':',$dsn);
parent::__construct($dsn_args[0],$user,$passwd,$dsn_args[1]);
}else{//config.inc.php --> config.db.php
parent::__construct(_DB_HOST_,_DB_USER_,_DB_PASSWD_,_DB_NAME_);
}
if (mysqli_connect_error()){
throw new ErrorException(mysqli_connect_error(),mysqli_connect_errno());
}
# ¹®ÀÚ¼Â
$chrset_is = parent::character_set_name();
if(strcmp($chrset_is,$chrset)) parent::set_charset($chrset);
}
#@ interface : ArrayAccess
# »ç¿ë¹ý : $obj["two"] = "A value";
public function offsetSet($offset, $value) {
$this->params[$offset] = parent::real_escape_string($value);
}
#@ interface : ArrayAccess
# »ç¿ë¹ý : isset($obj["two"]); -> bool(true)
public function offsetExists($offset) {
return isset($this->params[$offset]);
}
#@ interface : ArrayAccess
# »ç¿ë¹ý : unset($obj["two"]); -> bool(false)
public function offsetUnset($offset) {
unset($this->params[$offset]);
}
#@ interface : ArrayAccess
# »ç¿ë¹ý : $obj["two"]; -> string(7) "A value"
public function offsetGet($offset) {
return isset($this->params[$offset]) ? $this->params[$offset] : null;
}
# @ interface : DBSwitch
# :s1 -> :[¹®ÀÚŸÀÔ]+[º¯¼ö]
# :s[¹®ÀÚ], :d[Á¤¼ö], :f[¼Ò¼ö], :b[¹ÙÀ̳ʸ®]
# º¯¼öŸÀÔ, :s1,:sa,:sA, :d1, :d2, :dA ¾î¶»°Ôµç »ó°ü¾øÀ½
# ´Ü :s1, :s1 ÀÌ·¸°Ô Áߺ¹ µÇ¾î¼´Â ¾ÈµÊ
# where ±¸¹®¸¸ º¯°æ
// ("SELECT * FROM `TABLE` WHERE name=':s1' and age=':d2'",array('php',26,'³ª','¤¸¤§',22));
public function bindParams($query,$args=array()){
if(strpos($query,':') !==false){
preg_match_all("/(\:[s|d|f|b])+[0-9]+/s",$query,$matches);
if(is_array($matches))
{
foreach($matches[0] as $n => $s)
{
# ¹®ÀÚŸÀÔ°ú °ªÀÌ ÀÏÄ¡ÇÏ´ÂÁö üũ
$bindtype = substr($s,1,1);
$bvmatched = false;
switch($bindtype){
case 's': if(is_string($args[$n])) $bvmatched = true; break;
case 'd': if(is_int($args[$n])) $bvmatched = true; break;
case 'f': if(is_float($args[$n])) $bvmatched = true; break;
case 'b': if(is_binary($args[$n])) $bvmatched = true; break;
}
if($bvmatched){
$query = str_replace($s,'%'.$bindtype,$query);
$query = sprintf("{$query}",parent::real_escape_string($args[$n]));
}else{
$query = false;
break;
}
}
}
}
return $query;
}
#@ return int
# ÃѰԽù° °¹¼ö ÃßÃâ
public function get_total_record($table, $where="", $field='*'){
$wh = ($where) ? " WHERE ".$where : '';
if($result = parent::query('SELECT count('.$field.') FROM `'.$table.'`'.$wh)){
$row = $result->fetch_row();
return $row[0];
}
return 0;
}
// ÇϳªÀÇ ·¹ÄÚµå °ªÀ» °¡Á®¿À±â
public function get_record($field, $table, $where){
$where = ($where) ? " WHERE ".$where : '';
$qry = "SELECT ".$field." FROM `".$table."` ".$where;
if($result = $this->query($qry)){
$row = $result->fetch_assoc();
return $row;
}
return false;
}
# @ interface : DBSwitch
public function query($query){
$result = parent::query($query);
if( !$result ){
throw new ErrorException(mysqli_error(&$this).' '.$query,mysqli_errno(&$this));
}
return $result;
}
# @ interface : DBSwitch
# args = array(key => value)
# args['name'] = 1, args['age'] = 2;
public function insert($table){
$fieldk = '';
$datav = '';
if(count($this->params)<1) return false;
foreach($this->params as $k => $v){
$fieldk .= sprintf("`%s`,",$k);
$datav .= sprintf("'%s',", parent::real_escape_string($v));
}
$fieldk = substr($fieldk,0,-1);
$datav = substr($datav,0,-1);
$this->params = array(); #º¯¼ö°ª ÃʱâÈ
$query = sprintf("INSERT INTO `%s` (%s) VALUES (%s)",$table,$fieldk,$datav);
$this->query($query);
}
# @ interface : DBSwitch
public function update($table,$where)
{
$fieldkv = '';
if(count($this->params)<1) return false;
foreach($this->params as $k => $v){
$fieldkv .= sprintf("`%s`='%s',",$k,parent::real_escape_string($v));
}
$fieldkv = substr($fieldkv,0,-1);
$this->params = array(); #º¯¼ö°ª ÃʱâÈ
$query = sprintf("UPDATE `%s` SET %s WHERE %s",$table,$fieldkv,$where);
$this->query($query);
}
# @ interface : DBSwitch
public function delete($table,$where){
$query = sprintf("DELETE FROM `%s` WHERE %s",$table,$where);
$this->query($query);
}
# »ó¼ÓÇÑ ºÎ¸ð ÇÁ¶óÆÛƼ °ª Æ÷ÇÔÇÑ °¡Á®¿À±â
public function __get($propertyName){
if(property_exists(__CLASS__,$propertyName)){
return $this->{$propertyName};
}
}
# db close
public function __destruct(){
parent::close();
}
}
? >
¿©±â¼ ºÎÅÍ´Â
member Å×À̺íÀÌ ÀÖ´Ù°í °¡Á¤ÇÏ°í
uid : ȸ¿ø°íÀ¯¹øÈ£ userid : ¾ÆÀ̵ð name : À̸§ passwd : ºñ¹Ð¹øÈ£ email : À̸ÞÀÏ Á¤º¸ hp : ÈÞ´ëÆù
1. µ¥ÀÌŸ ÀÔ·Â
2. µ¥ÀÌŸ Äõ¸®
3. µ¥ÀÌŸ ¼öÁ¤
4. µ¥ÀÌŸ »èÁ¦ ¿¹¹®À» º¸°Ú½À´Ï´Ù.
1. insert µðºñ ÀúÀå /-------------------
< ?php
# Ŭ·¡½º ¼±¾ð
$db = new DbMySqli();
# µðºñ ÀúÀå
$db['uid'] = 1;
$db['userid'] = 'apmsoft';
$db['passwd'] = 'pwd1004';
$db['name'] = '¸ÚÀïÀÌ';
$db['email'] = 'apmsoft@test.com';
$db['hp'] = '010-3456-9876';
$db->insert();
? >
2. query µðºñ Äõ¸® /---------------------------
/**
ÀϹÝÀûÀÎ º¹¼öÀÇ µ¥ÀÌŸ Äõ¸®¹®
*/
< ?php
# µðºñ ¼±¾ð
$db = new DbMySqli();
# Äõ¸®
$result = $db->query("SELECT * FROM `member` WHERE uid='1'");
while( $row = $result->fetch_assoc() )
{
echo $row['uid'];
echo $row['userid'];
echo $row['name'];
}
? >
/**
´Ü¼ø µ¥ÀÌŸ Äõ¸®¹®
*/
< ?php
# µðºñ ¼±¾ð
$db = new DbMySqli();
# Äõ¸®
$row = $db->get_record('*', 'member', "uid='1'");
echo $row['uid'];
echo $row['userid'];
echo $row['name'];
? >
3. update µðºñ Á¤º¸ ¼öÁ¤ /----------------------------------
< ?php
# µðºñ¼±¾ð
$db = new DbMySqli();
# µðºñÁ¤º¸ ¼öÁ¤
$db['name'] = 'È«±æµ¿';
$db['email'] = 'ddd@gmail.com';
$db->update('member', "uid='1'");
? >
4. delete µðºñ »èÁ¦ /----------------------------------------
< ?php
# µðºñ¼±¾ð
$db = new DbMySqli();
# µðºñÁ¤º¸ »èÁ¦Çϱâ
$db->delete('member', "uid='1'");
? > |
|
̵̧ : 490 |
̵̧
¸ñ·Ï
|
|
| |
|