åé¡ã«çŽæ¥é¢ä¿ããªããå€å°é·ãå
責äºé
äºå®ãèªèããŸãããïŒæè¿Habréã«ç»å ŽããSQLã€ã³ãžã§ã¯ã·ã§ã³ã«å¯Ÿããä¿è·ã®ãããã¯ã«é¢ããèšäºïŒããã³ã³ã¡ã³ãïŒã®æ°ã¯ãäžéšã®äººãä¿¡ããŠããããã«ãã¯ãªã¢ã¯ããã»ã©å€ãã¯ãªãããšã瀺ããŠããŸãã ããã«ãåãééãã®ç¹°ãè¿ãã¯ãããã€ãã®èª€è§£ãå®å®ããããŠããããšã瀺åããŠãããæšæºçãªææ³ã®ãªã¹ãã ãã§ãªããããããã©ã®ããã«æ©èœããã©ã®å Žåã«é©çšãããã¹ããïŒããã³ããã§ãªãå ŽåïŒã®è©³çŽ°ãªèª¬æãå¿
èŠã§ãã
ãã®èšäºã¯éåžžã«é·ãããšãå€æããŸãã-ããã¯æ°å¹Žã«ãããç 究ã®çµæãå«ãã§ããŸã-ããããç§ã¯æåã«æãéèŠãªæ å ±ãç°¡æœã«æ瀺ããæåŸã«ããŸããŸãªå¥œå¥å¿ãšå¥åŠãªäºå®ã ãã§ãªãããã詳现ãªè°è«ãšã€ã©ã¹ããæäŸããããšããŸãã ãŸãã泚å°ã«å¯Ÿããä¿è·ã®ãããã¯ã«é¢é£ããè€æ°ã®èª€è§£ãšè¿·ä¿¡ãæçµçã«ææããããšããŸãã
ããªã°ããããæããŠããã¹ãŠã®ããŒã¿ããŒã¹ãšèšèªã«é¢ããæšå¥šäºé ãäžåºŠã«æžãããšã¯ããŸããã PHP / MySQLã®æã§ã®Webéçºã®ã¿ã§ååãªçµéšããããŸãã ãããã£ãŠããããã®æè¡ã«ã€ããŠã¯ããã¹ãŠã®å®çšçãªäŸãšæšå¥šäºé ãæäŸãããŸãã ããã«ããããããã以äžã«èª¬æããçè«äžã®ååã¯ããã¡ãããä»ã®èšèªãDBMSã«ãé©çšã§ããŸãã
ORMãã¢ã¯ãã£ãã¬ã³ãŒãããã®ä»ã®ã¯ãšãªãã«ããŒã«é¢ããæšæºçãªçºèšã«ããã«çããŸãããŸãããããã®ãã°ãããããŒã«ã¯ãã¹ãŠãæµ·ããã®éæ³ã®æ³¡ããçãŸãããã®ã§ã¯ãªããåã眪深ãSQLã䜿çšããŠããã°ã©ããŒã«ãã£ãŠæžãããŠããŸãã 次ã«ãçŸå®çã§ãããªã¹ããããŠããæè¡ã¯åªããŠããŸãããå®éã«ã¯ãã¬ã¬ã·ãŒã³ãŒãã§ãORMã«ç¿»èš³ãããJOIN JOINã§ããçã®SQLã«ã¯åžžã«åºäŒã£ãŠããŸãã ã§ããããç§ãã¡ã¯é ãç ã®äžã«é ããåé¡ããªããµããããŸããã
ç§ã¯ãã¹ãŠã®ãã¥ã¢ã³ã¹ã詳现ã«ã«ããŒããããšããŸããããç§ã®çµè«ã®ããã€ãã¯æçœã§ã¯ãªãããã«æããããããããŸããã ç§ã®ã³ã³ããã¹ããšèªè ã®ã³ã³ããã¹ããç°ãªãå Žåãããããšãå®å šã«èªããŸãã ãããŠãç§ã«ãšã£ãŠåœããåã®ããã«æãããããšã¯ãäžéšã®èªè ã®ããã§ã¯ãããŸããã ãã®å Žåãç§ã¯èšäºãä¿®æ£ããããç解ãããããæçãªãã®ã«ããã®ã«åœ¹ç«ã€è³ªåãšèª¬æã«åãã§ããŸãã
ãã®èšäºã¯éåžžã«é·ãããšãå€æããŸãã-ããã¯æ°å¹Žã«ãããç 究ã®çµæãå«ãã§ããŸã-ããããç§ã¯æåã«æãéèŠãªæ å ±ãç°¡æœã«æ瀺ããæåŸã«ããŸããŸãªå¥œå¥å¿ãšå¥åŠãªäºå®ã ãã§ãªãããã詳现ãªè°è«ãšã€ã©ã¹ããæäŸããããšããŸãã ãŸãã泚å°ã«å¯Ÿããä¿è·ã®ãããã¯ã«é¢é£ããè€æ°ã®èª€è§£ãšè¿·ä¿¡ãæçµçã«ææããããšããŸãã
ããªã°ããããæããŠããã¹ãŠã®ããŒã¿ããŒã¹ãšèšèªã«é¢ããæšå¥šäºé ãäžåºŠã«æžãããšã¯ããŸããã PHP / MySQLã®æã§ã®Webéçºã®ã¿ã§ååãªçµéšããããŸãã ãããã£ãŠããããã®æè¡ã«ã€ããŠã¯ããã¹ãŠã®å®çšçãªäŸãšæšå¥šäºé ãæäŸãããŸãã ããã«ããããããã以äžã«èª¬æããçè«äžã®ååã¯ããã¡ãããä»ã®èšèªãDBMSã«ãé©çšã§ããŸãã
ORMãã¢ã¯ãã£ãã¬ã³ãŒãããã®ä»ã®ã¯ãšãªãã«ããŒã«é¢ããæšæºçãªçºèšã«ããã«çããŸãããŸãããããã®ãã°ãããããŒã«ã¯ãã¹ãŠãæµ·ããã®éæ³ã®æ³¡ããçãŸãããã®ã§ã¯ãªããåã眪深ãSQLã䜿çšããŠããã°ã©ããŒã«ãã£ãŠæžãããŠããŸãã 次ã«ãçŸå®çã§ãããªã¹ããããŠããæè¡ã¯åªããŠããŸãããå®éã«ã¯ãã¬ã¬ã·ãŒã³ãŒãã§ãORMã«ç¿»èš³ãããJOIN JOINã§ããçã®SQLã«ã¯åžžã«åºäŒã£ãŠããŸãã ã§ããããç§ãã¡ã¯é ãç ã®äžã«é ããåé¡ããªããµããããŸããã
ç§ã¯ãã¹ãŠã®ãã¥ã¢ã³ã¹ã詳现ã«ã«ããŒããããšããŸããããç§ã®çµè«ã®ããã€ãã¯æçœã§ã¯ãªãããã«æããããããããŸããã ç§ã®ã³ã³ããã¹ããšèªè ã®ã³ã³ããã¹ããç°ãªãå Žåãããããšãå®å šã«èªããŸãã ãããŠãç§ã«ãšã£ãŠåœããåã®ããã«æãããããšã¯ãäžéšã®èªè ã®ããã§ã¯ãããŸããã ãã®å Žåãç§ã¯èšäºãä¿®æ£ããããç解ãããããæçãªãã®ã«ããã®ã«åœ¹ç«ã€è³ªåãšèª¬æã«åãã§ããŸãã
泚å°ã«å¯Ÿããä¿è·ã®ãããã¯ã«èå³ãæã¡å§ãããšããç§ã¯åžžã«å æ¬çã§ã³ã³ãã¯ããªã«ãŒã«ã®ã»ãããçå®ãããã£ãã®ã§ãã æéãçµã€ã«ã€ããŠãç§ã¯æåããŸããïŒ
泚å°ããç§ãã¡ãä¿èšŒããã«ãŒã«
- ãã¬ãŒã¹ãã«ããŒãä»ããŠã®ã¿ãªã¯ãšã¹ãã«ããŒã¿ãä»£å ¥ããŸã
- ã³ãŒãã§èŠå®ãããŠãããã¯ã€ããªã¹ãã®èå¥åãšããŒã¯ãŒãã®ã¿ã眮æããŸãã
2ç¹ã®ã¿ã
ãã¡ããããããã®ã«ãŒã«ã®å®çšçãªå®è£ ã«ã¯ããã詳现ãªã«ãã¬ããžãå¿ èŠã§ãã
ãããããã®ãªã¹ãã«ã¯å€§ããªã¡ãªããããããŸã-æ£ç¢ºã§å æ¬çã§ãã ãmysql_real_escape_stringãä»ããŠãŠãŒã¶ãŒå ¥åãå®è¡ããããŸãã¯å€§è¡æèã«æ ¹ããããåžžã«æºåãããåŒã䜿çšãããã«ãŒã«ãšã¯ç°ãªããç§ã®ã«ãŒã«ã»ããã¯å£æ» çãªèª€acyïŒæåã®ãããªïŒãŸãã¯äžå®å šãªïŒ2çªç®ã®ãããªïŒã§ã¯ãããŸããã
ããããå ã«é²ã¿ãŸããã-詳现ãªåæã«ç§»ããŸãããã
ãã¬ãŒã¹ãã«ããŒ-ããŒã¿çœ®æ
ååãšããŠããã¹ãŠãã·ã³ãã«ã§ããããŒã¿ã¯ãªã¯ãšã¹ãã«çŽæ¥å ¥åãããã¹ãã§ã¯ãªããäœããã®ä»£è¡šçãªã¯ã€ã«ãã«ãŒãè¡šçŸãä»ããŠååŸãããå¿ èŠããããŸãã
ãªã¯ãšã¹ãã¯ãããšãã°ã
SELECT * FROM table WHERE id > ? LIMIT ?
ããŒã¿ã¯åå¥ã«è¿œå ããã³åŠçãããŸãã
ãããããéåžžã®ãšã¹ã±ãŒãããããåªããŠããã®ã¯äœã§ããïŒ ãããŠçã«ïŒ
- ãŸããããšã¹ã±ãŒããã¯ã»ãã¥ãªãã£ãšã¯ãŸã£ããé¢ä¿ãããŸããã æ³åããŠã¿ãŠïŒ
- 第äºã«ãã³ãŒããçããªããŸãã mysql_real_escape_stringïŒïŒãintvalïŒïŒããããŸãã-ãã¹ãŠã®åŠçã¯å éšã«é ãããŠããŸãã
- 第äžã«ãã³ãŒããç°¡åã«ãªã£ãŠããŸãã ãªã¯ãšã¹ãã®ç°ãªãéšåããã©ãŒãããããããã®ç°ãªãã«ãŒã«ãèŠããå¿ èŠã¯ãããŸãã
- 第4ã«ããã¬ãŒã¹ãã«ããŒã䜿çšãããšïŒãã¬ãŒã¹ãã«ããŒãæ£ããåŠçãããå ŽåïŒãããŒã¿ãä»ããã€ã³ãžã§ã¯ã·ã§ã³ãä¿èšŒãããŸãã ãããã¯æããã§ãïŒã-ããªãã¯èšã-ãå¥ã®ãã€ã³ãããã§ã³ã¹ããããšã¯äŸ¡å€ããããŸãããïŒã åããšã¹ã±ãŒãä¿è·ã¯ä¿èšŒããŸããã ããã«ããã©ãŒãããèŠåã1ã€ã«æžãã-ããã§äœããæ··ä¹±ãããã®ã¯é£ããã ãã
- 5çªç®ããããŠæãéèŠãªããšã¯ãããŒã¿ãå¿ èŠãªå Žæã§æ£ç¢ºã«åŠçããããšã§ãïŒ ããã¯å€ãã®äººãç解ããŠããªãéåžžã«éèŠãªãã€ã³ãã§ãã å€å žçãªã楜ãããæç§æžã§ã¯ãSQLã®ããŒã¿åœ¢åŒã¯ã³ãŒãå šäœã«æ£ãã°ã£ãŠããŸãã ãŸããPHPã®å€ãããŒãžã§ã³ã§ã¯ãã³ãŒãå®è¡ã®éå§åã§ããéå§ãããŠããŸãããããã¯ããŸã£ããã²ãŒãã«ã¯å ¥ããŸããïŒ ãã®ç¶æ³ã§ã¯ãå¿ èŠã«å¿ããŠãããããªå©ç¹ãªãã«ãäžéšã®ããŒã¿ã2åãã©ãŒããããããä»ã®ããŒã¿ãååã ããããã«ã¯ä»ã®ããŒã¿ããŸã£ãããŸãã¯ãŸã£ãããã©ãŒããããããªããšããäºå®ã«ã€ãªãããŸãã åæã«ãSQLçšã«ãã©ãŒããããããããŒã¿ãHTMLãŸãã¯Cookieã«çªç¶è¡šç€ºãããŸãããããããŠãŒã¶ãŒãéçºè ã«åã³ãäžããŸããã
ãããã£ãŠãæé©ãªãªãã·ã§ã³ã¯ããªã¯ãšã¹ããå®è¡ããçŽåã«ããŒã¿ããã©ãŒãããããããšã§ã-ãã®ããã«ããŠãããŒã¿ãæ£ãããã©ãŒããããããŠããããšãåžžã«ç¢ºèªããŸããããã¯äžåºŠã ãè¡ããããã©ãŒããããããããŒã¿ã¯æå³ãããšããã«ãªããŸã-ããŒã¿ããŒã¹ã«ã©ãã«ãè¡ããªã
ãããŠãããã¯ãŸãã«ãã®ãããª-ã¿ã€ã ãªãŒã§å®å šã§æ£ãã-ããŒã¿åŠçã®ç®æšã§ããããã¬ãŒã¹ãã«ããŒãæäŸããããšã§ãã»ãã¥ãªãã£ãä¿èšŒãããšåæã«ã³ãŒããç°¡çŽ åããŸãã
䜿çšäŸïŒ
ããã¥ã¢ã«ããïŒ
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name LIKE ?"); $stmt->execute(array("%$_GET[name]%")); $data = $stmt->fetchAll();
åªåãã¹ãäŸïŒ
$ban = $db->getRow("SELECT 1 FROM ban WHERE ip = inet_aton(s:)", $ip);
ã芧ã®ãšãããé£ããããšã§ã¯ãããŸããããŸããå·§ã¿ã«äœ¿çšããã°ãæã§ãªã¯ãšã¹ããè¡ããããã¯ããã«çããªããŸãã ããªãã¯ãŸã æãªããã®æ¹æ³ã§æžãããã§ããïŒ
éèŠãªæ³šæïŒãã¡ãããããŒã¿ãœãŒã¹ããã®ä»ã®æ¡ä»¶ã«é¢ä¿ãªãããã¬ãŒã¹ãã«ããŒãä»ããããŒã¿çœ®æã¯åžžã«å®è¡ããå¿ èŠããããŸãã
èå¥åãšããŒã¯ãŒã-ãã¯ã€ããªã¹ã
泚å°ã«é¢ããèšäºã®å€§éšåã¯ããã®ç¹ãå®å šã«èŠéããŠããŸãã ãããçŸå®ã«ã¯ãã¯ãšãªã§ã¯ããŒã¿ã ãã§ãªããä»ã®èŠçŽ ïŒèå¥åïŒãã£ãŒã«ããšããŒãã«ã®ååïŒãããã«ã¯æ§æèŠçŽ ãããŒã¯ãŒãïŒã眮æããå¿ èŠããããŸãã DESCãANDã®ããã«éèŠã§ã¯ãªãå Žåã§ãããã®ãããªçœ®æã®ã»ãã¥ãªãã£èŠä»¶ã¯ãããã»ã©å³ããã¯ãªãã¯ãã§ãïŒ
ããªãå¹³å¡ãªã±ãŒã¹ã調ã¹ãŠã¿ãŸãããã
補åã®ããŒã¿ããŒã¹ããããããã¯HTMLããŒãã«ã®åœ¢åŒã§ãŠãŒã¶ãŒã«è¡šç€ºãããŸãã ãŠãŒã¶ãŒã¯ããã®ããŒãã«ãããããã®ãã£ãŒã«ãã§ä»»æã®æ¹åã«äžŠã¹æ¿ããããšãã§ããŸãã
ã€ãŸããå°ãªããšããŠãŒã¶ãŒåŽããã¯ãååãšäžŠã¹æ¿ãæ¹åãååŸããŸãã
ããããèŠæ±ã«çŽæ¥ä»£å ¥ããããšã¯ãä¿èšŒãããæ³šå ¥ã§ãã éåžžã®ãã©ãŒãããæ¹æ³ã¯ããã§ã¯åœ¹ã«ç«ã¡ãŸããã èå¥åãããŒã¯ãŒãããªãæºåãããåŒã¯ããšã©ãŒã¡ãã»ãŒãžä»¥å€ã¯äœãå°ããŸããã
å¯äžã®è§£æ±ºçã¯ãã¯ã€ããªã¹ãã§ãã
ãã¡ãããããã¯ãã¥ãŒãã³ã®äºé åŒã§ã¯ãªããå€ãã®éçºè ã¯å€åºå ã§ãã®ãã©ãã€ã ãç°¡åã«å®è£ ããŸããæåã«ã¯ãšãªã®ãã£ãŒã«ãåã眮ãæããå¿ èŠããããŸããã ãã ãããã®èŠåã®ãªãæ³šå ¥ä¿è·ã«é¢ããèšäºã¯äžå®å šã§ãããä¿è·èªäœã¯æŒãããããã®ã§ãã
ãã®ã¡ãœããã®æ¬è³ªã¯ã ãã¹ãŠã®å¯èœãªéžæè¢ãã³ãŒãã«ãã£ãããšèšè¿°ãããŠãŒã¶ãŒã®å ¥åã«åºã¥ããŠãããã®ã¿ããªã¯ãšã¹ãã«å«ããå¿ èŠãããããšã§ãã
é©çšäŸïŒ
$order = isset($_GET['order']) ? $_GET['order'] : ''; // $sort = isset($_GET['sort']) ? $_GET['sort'] : ''; $allowed = array("name", "price", "qty"); // $key = array_search($sort,$allowed); // $orderby = $allowed[$key]; // (, - ) . $order = ($order == 'DESC') ? 'DESC' : 'ASC'; // $query = "SELECT * FROM `table` ORDER BY $orderby $order"; // 100%
以åã¯ããã¬ãŒã¹ãã«ãã§èå¥åã«ååã§ãããšæ³å®ããŠããŸããã ããããæéãçµã€ã«ã€ããŠããã®æ¹æ³ã®æ¬ ç¹ãç解ãããããã«ãªããŸããã
- ãŸããç¡å¹ãªãã£ãŒã«ãåã®å Žåããªã¯ãšã¹ãã«ãããšã©ãŒãçºçããŸãã ãããŠééãã¯åžžã«æªã
- 第äºã«ãã¯ããã«éèŠã§ãïŒäºåãã£ã«ã¿ãªã³ã°ãªãã§ãã£ãŒã«ãåãèªåçã«çœ®æããå Žåãèªåçã«ãšã¹ã±ãŒãããã ãã§ãç°ãªãçš®é¡ã®ã€ã³ãžã§ã¯ã·ã§ã³ãååŸã§ããŸã-çµå±ããŠãŒã¶ãŒã¯å€æŽããªãã¯ãã®ãã£ãŒã«ãåãå ¥åã§ããŸãïŒ ããšãã°ã$ _POSTé åã«åºã¥ããŠSQLã¯ãšãªãèªåçã«çæããå ŽåïŒãã£ãŒã«ãåãæ£ãããã©ãŒãããããŠããéã«ïŒïŒãããã«ãŒã¯ãç»é²æã«å€ã1ãã®adminãã£ãŒã«ãããã©ãŒã ã«è¿œå ãã管çè ã«ãªããŸã
ã ããä»ãç§ã¯äž¡æ¹ã®æ¹æ³ã䜿çšããŸãïŒ
æåã«ããã¯ã€ããªã¹ãããèå¥åãååŸããŸãã
ãããŠããã¬ãŒã¹ãã«ããŒãä»ããŠè¿œå ããŸã-æåã®æžåŒèšå®ãåŠçããªãããã«ããããã§ãã ãããŠåäžæ§ã®ããã«ã ãã®å ŽåãæåŸã®è¡ã¯æ¬¡ã®ããã«ãªããŸã
$query = "SELECT * FROM `table` ORDER BY n: $order";
åºæ¬çã«ããã®æ å ±ã¯å®å šã«å®å šãªã¯ãšãªãæžãå§ããã®ã«ååã§ãã ãããããã€ãã®ããã«ãå®éã®ç掻ã«ã¯ããŸããŸãªãã¥ã¢ã³ã¹ãããããã¬ãŒã¹ãã«ããŒã®ã¡ã«ããºã ããã詳现ã«ç解ããããšæããŸãã
ãã¬ãŒã¹ãã«ããŒã䜿çšãã
æåã«ããã¬ãŒã¹ãã«ããŒãå®è£ ããããã®2ã€ã®ãªãã·ã§ã³ïŒãµãŒããŒãšã¯ã©ã€ã¢ã³ãïŒãããããšãç解ããå¿ èŠããããŸãã
- æåã®ã±ãŒã¹ã§ã¯ããªã¯ãšã¹ãã¯ãã¬ãŒã¹ãã«ããŒãšãšãã«ãµãŒããŒã«éä¿¡ãããããŒã¿ã¯ãããšã¯å¥ã«éä¿¡ãããŸãã è±èªã§ã¯ãããã€ãã£ããæºåæžã¿ã¹ããŒãã¡ã³ã-ããã€ãã£ããæºåæžã¿ã¹ããŒãã¡ã³ããšåŒã°ããŸããã€ãŸãããã¬ãŒã¹ãã«ããŒã®åŠçã¯ããµãŒããŒäžã®DBMSèªäœã«ãã£ãŠå®è¡ãããŸãã ç°¡æœã«ããããã«ãããŒã ãµãŒããŒãµãŒããŒã®ãã¬ãŒã¹ãã«ããŒã䜿çšããŸãã
- 2çªç®ã®å ŽåãããŒã¿ã¯ãã©ãŒããããããã¯ã©ã€ã¢ã³ãäžã®çŽæ¥ã®ãã¬ãŒã¹ãã«ããŒã®ä»£ããã«ã¯ãšãªæååã«çœ®æãããåŸæ¥ã®SQLã¯ãšãªã圢æããéåžžã®æ¹æ³ã§ããŒã¿ããŒã¹ã«éä¿¡ãããŸãã
ãŸããPDOã¯ãããã®2ã€ã®æ€ åã«åæã«åº§ã£ãŠããããšãèŠããŠããå¿ èŠããããŸããããã©ã«ãã§ã¯ã2çªç®ã®ãªãã·ã§ã³ã«åŸã£ãŠåäœããæåã®ãªãã·ã§ã³ã®ã¿ããšãã¥ã¬ãŒãããŸãã ãã®æ©èœãç¡å¹ã«ããã«ã¯ãPDOã«ãªã¯ãšã¹ããšã¯å¥ã«ãµãŒããŒã«ããŒã¿ãéä¿¡ãããå¿ èŠããããŸãã
$dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
ãã ãããšãã¥ã¬ãŒã·ã§ã³ããããã°ã©ããŒã®åå ãªãã§è¡ãããããã以äžã§ã¯PDOããµãŒããŒãã¬ãŒã¹ãã«ããŒã®ä»£è¡šãšããŠèããŸãã
ãµãŒããŒã®ãã¬ãŒã¹ãã«ããŒ
ãŸãå§ãã«ããªã泚å°ãå¯èœãªã®ã§ããããïŒ
å®éãSQLã¯ãšãªã¯ããã°ã©ã ã§ãã å®å šãªããã°ã©ã -æŒç®åãå€æ°ãããã³æååãªãã©ã«ã åé¡ã¯ãå€åºå ã§ãã®ããã°ã©ã ãåçã«åéããããšã§ãã äžåºŠã ãèšè¿°ãããçä¿¡ããŒã¿ã«åºã¥ããŠå€åããªãPHPã¹ã¯ãªãããšã¯ç°ãªããSQLã¯ãšãªã¯æ¯ååçã«åçæãããŸãã ãããŠããã®çµæã誀ã£ãŠãã©ãŒããããããããŒã¿ã¯ãªã¯ãšã¹ããå°ç¡ãã«ããããå Žåã«ãã£ãŠã¯å€æŽãããããŠãåœç€ŸãæäŸããŠããªãæŒç®åã眮ãæããŸãã å®éãããã¯ãŸãã«æ³šå°ã®æ¬è³ªã§ãã
ãã¬ãŒã¹ãã«ããŒã®ãµãŒããŒåŠçã¯äœãæäŸããŸããïŒ
éåžžã«ç°¡åãªããšïŒ... å€æ°ã®ãããªãã®ãããã°ã©ã ã«å°å ¥ããŸã ïŒ ã¯ãããã¬ãŒã¹ãã«ããŒã¯éåžžã®å€æ°ã§ãããSQLã®ãã¹ã¯ãªãããã«ããŒãã³ãŒãã£ã³ã°ãããŠãããããŒã¿ã«ãã£ãŠã¯å€åããŸããã ãŸããããŒã¿èªäœã¯ãªã¯ãšã¹ããšã¯å¥ã«ãµãŒããŒã«éããã決ããŠäº€å·®ããããšã¯ãããŸããã ãªã¯ãšã¹ãã解éãããåŸã«ã®ã¿ãããŒã¿ã¯å®è¡æ®µéã§çŽæ¥äœ¿çšãããŸãã
å®éã«ã¯ã次ã®ããã«ãªããŸãïŒprepareïŒïŒãåŒã³åºããšããªã¯ãšã¹ãã¯ãã®åœ¢åŒã§ãµãŒããŒã«çŽæ¥éãããŸã-ãã¬ãŒã¹ãã«ããŒ/å€æ°ã䜿çšããŠããµãŒããŒã¯ããã解æããããã¹ãŠãæ£åžžã§ãããŒã¿ãåä¿¡ããæºåãã§ããŠãããããšãéç¥ããŸãïŒãŸãããŸãã¯ãšã©ãŒãå ±åããŸãïŒã ãããŠãexecuteïŒïŒãå®è¡ããããšãããŒã¿ã¯ãã§ã«ãµãŒããŒã«éããïŒããã¹ã圢åŒã§ã¯ãªãããã€ããªããã±ãŒãžã§ãã¯ãšãªçµæãè¿ããããã®ãšæ§é ã䌌ãŠããŸãïŒããã§ã«å®è¡ã«çŽæ¥é¢äžããŠããŸãã
çè«çã«ã¯ãéåžžã«é åçã§ãã
ãã ããå®éã«ã¯ãæ®å¿µãªãããPHPã§Mysqlãæäœããããã®æ¢åã®ã©ã€ãã©ãªã§ã¯ãæºåãããåŒãæäœããå®è£ ã¯ãŸã çæ³ããã¯ã»ã©é ããã®ã§ãã
ããšãã°ã次ã®äºå®ãåŒçšããã ãã§ååã§ãã
- æºåãããåŒã䜿çšããã¯ãšãªã®çµææååãé åïŒmysql_fetch_arrayïŒïŒã®ã¢ããã°ïŒã«å ¥ãããªã©ã®ãããµããæ©èœã¯ãããŒãžã§ã³5.3ïŒïŒïŒã§ã®ã¿mysqliã«è¿œå ãããŸããã ãã®ããŒãžã§ã³ããåã¯ãã©ã€ãã©ãªã¯å®éã«ã¯äžå®å®ã§ã å°çã®ãããªåé¯ããããŸããã§ããã
- åãããšãPDOã§ã®æ¥ç¶ã®ãšã³ã³ãŒãã£ã³ã°ã®èšå®ã«ãé©çšãããŸã-ããã¯åã5.3ã§ã®ã¿å¯èœã«ãªããŸããã 以åã®ãã¹ãŠã®ããŒãžã§ã³ã§ã¯ãPDOã§æ¥ç¶ã®ãšã³ã³ãŒããæå®ããããšã¯åã«äžå¯èœã§ããïŒå ¬å¹³æ§ãèæ ®ããŠãäºææ§ã¢ãŒãããªãã«ãªã£ãŠããå Žåãããã¯å€§ããªåé¡ã§ã¯ãããŸããã-ç©ŽïŒïŒã
- mysqliã¯ããã£ãŒã«ããããŒã¿ããŒã¹ã«ã§ããã ãåãŸãããã«ãã§ããã ãå€ãã®ã¡ã¢ãªãäºçŽããããšããå ŽåããããŸããäžçšåºŠã®ããã¹ãã«ã¯æ³šæããŠãã ããã
æ¢åã®ã©ã€ãã©ãªã®äž»ãªæ¬ ç¹ããªã¹ãããŸãã
- åé·æ§
- ããã€ãã®æçšãªãã¬ãŒã¹ãã«ããŒã®æ¬ åŠ
- ãããã°ç®çã§åŸæ¥ã®SQLã¯ãšãªãååŸã§ããªã
- èããããããã©ãŒãã³ã¹ã®åé¡*ïŒãã®åé¡ã«ã€ããŠã¯ä»¥äžã§èª¬æããŸãïŒ
ãã詳现ã«åæããŸã
åé·æ§ã
ããšãã°ãã¯ãšãªçµæã®ãã¹ãŠã®è¡ã2次å é åã«å ¥ãããªã©ã®äžè¬çãªæäœãèããŠã¿ãŸãããã mysqliã«ã¯ãã®ãããªé¢æ°ã¯ãŸã ãããŸããã ãŸãã¯ãããšãã°ãå€æ°ããã¬ãŒã¹ãã«ããŒã«ãã€ã³ãã§ããã®ã¯ãå¥åã®é¢æ°ã®ã¿ã§ãã
ãã®çµæã1ã€ã®ãªã¯ãšã¹ãã®ããŒã¿ãé åã«ååŸããã«ã¯ãå°ãªããšã9è¡ïŒïŒïŒã®ã³ãŒããå¿ èŠã§ãã
$data = array(); $query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1"; $stmt->prepare($query); $stmt->bind_param("s", $continent); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_array(MYSQLI_NUM)) { $data[] = $row; }
ããã«ããã®ã³ãŒãã®ã»ãšãã©ã¯ã»ãã³ãã£ãã¯ããŒããæãããå®è¡ããããã¹ãŠã®ã¯ãšãªã§ãŸã£ããåãã§ãããäœçŸãã®ã¹ã¯ãªããã§äœåºŠãç¹°ãè¿ãããŸãã
ããŒã¿ãååŸããã«ã¯2è¡ã ãã§ååã§ãããšããäºå®ã«ããããããïŒ
$query = "SELECT Name, Population, Continent FROM Country WHERE Continent=? ORDER BY Name LIMIT 1"; $data = $db->getAll($query,$continent);
PDOã®æ¹ãããã«å°ãåªããŠããŸã-é åãå®è¡ïŒïŒã«æž¡ãããšãã§ããfetchAllïŒïŒã¡ãœããããããã©ãããããããŸããã ããããæãåçŽãªæäœã®ããã«ãäžå¿ èŠãªå調ãªã³ãŒããããããæžãå¿ èŠããããŸãã
ç¹ã«ãèšäºã®ãããã¯ã§ãããã€ã³ãã£ã³ã°ã«æ»ããŸãã ããã«å€æ°$ _GET ['id'];ããããŸãã ãã¬ãŒã¹ãã«ããŒã«æ·»ä»ããŸãã ãã°ããããexecuteïŒïŒã§çŽæ¥è¡ãããšãã§ããŸãããé åã«ããããšã«ãã£ãŠã®ã¿å¯èœã§ãã ãªãã§ïŒ ãã©ã€ããŒèªäœãç§ã®ããã«ãããè¡ãããšã¯ã§ããŸãããïŒ
æ©èœäžå š
æè¿ã®èšäºã®ã³ã¡ã³ãã§æ¢ã«æèµ·ãããŠããå¥ã®åé¡ã¯ãINïŒïŒæŒç®åã§ãã 眮æãè¡ãããšã¯éåžžã«ç°¡åãªäœæ¥ã§ãã ãã¬ãŒã¹ãã«ããŒãçºæãããã®ã¯ãŸãã«ãã®ãããªå Žåã§ããããã«æããŸããïŒ
$conts = array('Europe','Africa','Asia','North America'); $query = "SELECT * FROM Country WHERE Continent IN(?) ORDER BY Name LIMIT 1"; $data = $db->getAll($query,$conts);
æ°åè¡ã®äžèªç¶ãªã³ãŒãã®ä»£ããã«ã
ããããã©ã®ãããªçµè«ãå°ãåºãããšãã§ããŸããïŒ çµäºããå¿ èŠããããŸãã
SQLã¯ãšãªãååŸã§ããªã
誰ãã圌ã¯ãããå¿ èŠãšããªããšèšãã§ãããã誰ããåããå€ããŠåœŒã®æã§èŠæ±ãæžãããcãªãœãããŠã§ã¢ã䜿çšããŸãã ãããã®äººã ã®æèŠãå°éããŸãããäºå®ã¯æ®ããŸã-å®æãããªã¯ãšã¹ãã®åºåã®æ©èœã¯ãããã°ã«äŸ¿å©ã§ããããµãŒããŒã®ãã¬ãŒã¹ãã«ããŒã¯ãããèš±å¯ããŸããã
æ§èœ
éåžžããµãŒããŒåŽã§æºåãããåŒã®è¬çœªè ã¯ããªã¯ãšã¹ãã®è§£æãäžåºŠã ãè¡ããããšããäºå®ã匷調ããŠããŸãã
æ®å¿µãªãããWebã¢ããªã±ãŒã·ã§ã³ã®å Žåãããã¯æ©èœããŸããã prepareïŒïŒãå®è¡ããã¹ã¯ãªããã®ã³ããŒã¯ããã®ãªã¯ãšã¹ãã«å¯ŸããŠexecuteïŒïŒãæ£ç¢ºã«1åå®è¡ããå®å šã«çµäºããŸãã æ°ããã³ããŒãå床æºåãããŸãã ãã®çµæãä¿åãããå Žæã«ããå€ãã®äœæ¥ãçºçããŸãã
æè¿ã®èšäºã«å¯Ÿããã³ã¡ã³ãã§ãå¥ã®æœåšçãªé床åäž-ã¯ãšãªãã©ã³ã®ãã£ãã·ã¥-ãææãããŸããã å®éãåã¯ãšãªãæºåããŠããããŒã¿ããŒã¹ã¯ç°ãªãããŒã¿ã«å¯ŸããŠåãã¯ãšãªããã£ãã·ã¥ã§ããŸãã 解æããã«ãåçŽãªæååæ¯èŒã§å®è¡èšç»ãååŸããŸãïŒ
æ®å¿µãªãããç§ã¯MySQLã®å éšæ§é ã«ãããŠãã¡ã«ããºã èªäœã®ååšãšãã®å®çšçãªæå¹æ§ã®äž¡æ¹ã確èªãŸãã¯åŠå®ããã»ã©åŒ·ãã¯ãããŸããã
åæã«ãç§ãç¥ãéããå®éã®è² è·ã倧ãããšããµãŒããŒãæºåããåŒã¯æšæºã®SQLã¯ãšãªã®é床ã倱ããŸãã
ãããã«ããããããã¯ã¯å ¬éãããŠããããŸã ç 究è ãåŸ ã£ãŠããŸãã æçµçã«ãDBMSããŒãžã§ã³ã倧ãããªãããã¹ãçµæãå€ããªã£ãŠããŸããŸãã
äžè¬ã«ãDBMSãšãããæäœããããã®ãã©ã€ããŒãæäŸããããŒã«ã¯ãåºåã«èŠãããã»ã©è¯ããªãããšãããããŸããã ãããŠçåãçããŸã-èªåã§ãã¬ãŒã¹ãã«ããŒãšã®äœæ¥ãå®è£ ã§ããŸããïŒ ãããŠçããçãŸããŸã-ç§ãã¡ã¯ã§ããŸãïŒ
ãã¬ãŒã¹ãã«ããŒã®ç¬ç«ããå®è£
å®éããã¬ãŒã¹ãã«ããŒã«ç ©ããããããšãªããæ¢åã®ã©ã€ãã©ãªã®äœ¿ãããããæ¹åããããšã劚ãããã®ã¯èª°ãããŸããã ããšãã°ãç¬èªã®PDOãã¬ãŒã¹ãã«ããŒïŒãŸãã¯1幎åã«Habréã§å ¬éãããã©ã€ãã©ãªã§è¡ãããmysqliïŒã䜿çšããŠãäžè¶³ããŠããæ©èœãå®è£ ããPDOã®ã©ãããŒãäœæããŸãã
ãã ããèªå®¶è£œã®ãã¬ãŒã¹ãã«ããŒãæ€èšããçç±ã¯ããã€ããããŸãã
ãŸãããã§ã«èŠãããã«ãæšæºã©ã€ãã©ãªã§å©çšå¯èœãªãã¬ãŒã¹ãã«ããŒã®ã»ããã§ã¯æããã«ååã§ã¯ãããŸããã
第äºã«ãäœããã®çç±ã§ãµãŒããŒã®ãã¬ãŒã¹ãã«ããŒãé©åã§ãªãå ŽåããããŸãã
第äžã«ããã¬ãŒã¹ãã«ããŒã®ç¬ç«ããåŠçã®äŸã§ã¯ãSQLã¯ãšãªãæ£ãããã©ãŒãããããããšã®ãã¥ã¢ã³ã¹ãèæ ®ããŸãã
SQLã¯ãšãªã®ããŸããŸãªèŠçŽ ã®æžåŒèšå®ã®åå
èªå®¶è£œã®ãã¬ãŒã¹ãã«ããŒãå®è£ ããã®ã¯ç°¡åã§ãã ããªããã£ãããŒãµãŒã¯æ¢ã«PHPã«çµã¿èŸŒãŸããŠããŸãã å¿ èŠãªã®ã¯ããªã¯ãšã¹ãã®ããŸããŸãªèŠçŽ ãåºå¥ããæ¹æ³ãåŠã¶ããšã ãã§ãã ããããããã¯éåžžã«éèŠãªãã€ã³ãã§ããããã詳现ã«èª¬æããå¿ èŠããããŸãã
ãã©ãŒãããèŠåã¯èŠçŽ ã®ã¿ã€ãã«äŸåããããããŸããã©ã®ç¹å®ã®èŠæ±èŠçŽ ã«çœ®æããã®ããæ確ã«ç解ããå¿ èŠããããŸãã 次ã«ããã®æ å ±ãäœããã®æ¹æ³ã§ãã¬ãŒã¹ãã«ããŒããã»ããµã«äŒããå¿ èŠããããŸãã
ãŸããã¯ãšãªãæ§æããèŠçŽ ã決å®ããŸããïŒ
ããšãã°ã次ã®ãããªSQLãèããŸãã
INSERT INTO `db`.`table` as `t1` VALUES('string',1,1.5,NOW());
èŠçŽ ã®3ã€ã®äž»èŠãªã°ã«ãŒããåºå¥ã§ããŸãã
- SQLèšèªèŠçŽ èªäœ-æŒç®åãçµã¿èŸŒã¿é¢æ°ãå€æ°ãªã©
- èå¥åïŒããŒã¿ããŒã¹ãããŒãã«ãããã³ãã£ãŒã«ãã®ååïŒ
- ããã€ãã®ç°ãªãã¿ã€ãã®ãªãã©ã«ïŒã¯ãšãªã«çŽæ¥ä»£å ¥ãããããŒã¿ïŒã
次ã«ããã³ãã©ãŒã®ä»£ããã«äœ¿çšãããããŒã¿ã®ã¿ã€ãã«é¢ããæ å ±ãå ±åããæ¹æ³ã«ã€ããŠèããŠã¿ãŸãããã å©çšå¯èœãªãœãªã¥ãŒã·ã§ã³ã¯ããããæ²ããããã£ãŠå®è¡ããŸãïŒããã¯ãã¯ãé©ãããšã§ã¯ãããŸããïŒã 2ã€ã®ãªãã·ã§ã³ããããŸãïŒãã€ã³ãã£ã³ã°é¢æ°ãåŒã³åºãããšã«ãã£ãŠã¿ã€ããèšå®ããå¿ èŠãããïŒã³ãŒããæã ããã«è€éã«ããïŒããå®è¡ããããã«ããŒã¿ãçŽæ¥æž¡ãå ŽåïŒPDOã®ããã«ãŸã£ããèšå®ããªãïŒã ãã ããåãªãã§ã¯å®è¡ã§ããªããããå®è¡ããããã«è»¢éããããã¹ãŠã®PDOããŒã¿ã¯æååãšããŠæ±ãããŸãã PDOãäºæã¢ãŒãã§åäœããå Žåãããã¯é¢çœãçµæã«ã€ãªãããŸãïŒLIMITãå®è¡ããããã®ãã©ã¡ãŒã¿ãŒãæž¡ãããšãããšïŒïŒãPDOã¯ãšã©ãŒã¡ãã»ãŒãžã§ã¯ã©ãã·ã¥ããŸãã
äžè¬ã«ãå¥ã®ãœãªã¥ãŒã·ã§ã³ãå¿ èŠã§ãã ãããŠããã¯ïŒ ãã ãã以äžã§èª¬æããŸãããä»ã®ãšããã¯ãã©ãŒãããã«ãŒã«ã«ã€ããŠæ€èšããŸãã
èå¥åã®ãã©ãŒããã
äžè¬ã«ãèå¥åã®åœåèŠåã¯éåžžã«åºç¯å²ã§ãã ãã ããã»ãã¥ãªãã£ã®ããã«ããã¯ã€ããªã¹ããšãã©ãŒãããã®ã¿ã®ãã¬ãŒã¹ãã«ããŒã䜿çšããããšãèæ ®ãããšã次ã®2ã€ã§ååã§ãã
- èå¥åã¯ãäžéåŒçšç¬ŠïŒããã¯ã¯ã©ãŒãïŒã§å²ãå¿ èŠããããŸã
- ãã®ãããªåŒçšç¬Šãååã«å«ãŸããå Žåãäºéã«ããããšã§ãšã¹ã±ãŒãããå¿ èŠããããŸãã
function escapeIdent($value) { return "`".str_replace("`","``",$value)."`"; }
åæ çãªäœè«ïŒ
èå¥åããã©ãŒãããããå¿ èŠã¯ãããŸããïŒ å®éãã»ãšãã©ã®å Žåãããã¯å¿ èŠãããŸãããïŒ
ãªã¯ãšã¹ããææžãã§æžãããŠããå Žåãå¿ èŠæ§ã¯ãã®å Žã§å€æã§ããŸãããªã¯ãšã¹ãã¯æ©èœããŸã-ãã©ãŒãããããããšã¯ã§ããŸããã èå¥åã®ãšã©ãŒã§ã¯ã©ãã·ã¥ããŸã-ãã©ãŒãããããå¿ èŠããããŸãã
ãã¬ãŒã¹ãã«ããŒã䜿çšããå Žåãã€ãŸãèå¥åããªã¯ãšã¹ãã«åçã«è¿œå ããå Žåããªã¯ãšã¹ãã§ã©ã®ãã£ãŒã«ãåã眮æããããããããã£ãŠãã©ãŒããããå¿ èŠãã©ãããããããªãããããã©ãŒãããããå¿ èŠããããŸãã ãããã£ãŠããã¹ãŠããã©ãŒãããããŸãã
å®éãäžè¬çãªãã¬ãŒã¹ãã«ããŒã®äœ¿çšã«ã€ããŠè©±ãå Žåãäžè²«æ§ã®ããäŸå€ãªããã©ãŒãããèŠåãé©çšããããšã§ãã€ã³ãžã§ã¯ã·ã§ã³ããã³éèŠãªãšã©ãŒã«å¯Ÿããä¿è·ã®ä¿èšŒã«ã€ããŠè©±ãããšãã§ããŸãã çµå±ããã©ãŒãããã¯äž»ã«ãªã¯ãšã¹ãã®æ§æã®æ£ç¢ºããä¿èšŒããããã«è¡ãããŸãã ãããŠã泚å°ã«å¯Ÿããä¿è·ã¯åãªãå¯äœçšã§ãã
ãããã£ãŠãããšã¹ã±ãŒãããããããã©ãŒãããããšããçšèªã䜿çšããããšã奜ã¿ãŸã
ãã©ãŒãããæååãªãã©ã«
æãããã¯ããããããã¯ã®ããã«æããŸããããããæè¿ã®èšäºã®è°è«ã瀺ããŠããããã«ãå€ãã¯ç解ããŠããªããŠããå°ãªããšãèšèé£ãã§ã¯ãŸã æ··ä¹±ããŠããŸããããã§ã¯ãSQLã§æååããã©ãŒãããããããã®ã«ãŒã«ãå®åŒåããŸãããã
- æååã¯åŒçšç¬Šã§å²ãå¿ èŠããããŸãïŒåäžãŸãã¯äºéã§ãããèå¥åã«ã¯äºéã䜿çšã§ãããããåžžã«åäžã䜿çšããããšããå§ãããŸãïŒ
- è¡ã®ãªã¹ãã®ç¹æ®æåããšã¹ã±ãŒãããå¿ èŠããããŸãããã®ãããAPIã¯ç¹å¥ãªæ©èœãæäŸããŸãããã®é¢æ°ãæ£ããæ©èœããã«ã¯ãæ¥ç¶ãšã³ã³ãŒããæ£ããèšå®ãããŠããå¿ èŠããããŸãã
ããªãã
- ãããã®ã«ãŒã«ã¯ã1ã€ã ãã§ãªããåžžã«äžç·ã«é©çšããå¿ èŠããããŸã
- ãããã®ããããè¡ä»¥å€ã®ããŒã¿ã«ã¯é©çšããªãã§ãã ãã
ããã¯åçŽãªã«ãŒã«ã®ããã§ããïŒããããã©ãã ãå€ãã®äººããã¹ãã§ã¯ãªããã¯é©ãã¹ãããšã§ãã
PHPããã¥ã¡ã³ãã®mysql_real_escape_stringïŒïŒã«é¢ããèšäºã§ããã²ãŒã ã¯æ¬¡ã®ããã«è¿°ã¹ãŠããŸããããã®é¢æ°ã䜿çšããªããšãã¯ãšãªã¯SQLã€ã³ãžã§ã¯ã·ã§ã³ã«ãããããã³ã°ã«å¯ŸããŠè匱ã«ãªããŸããã-æ°å€ãŸãã¯èå¥åã«äœ¿çšãããšãå°ãªããšãäœãã«åœ¹ç«ã€ããã«ïŒ
ããããPDOéçºè ã¯è³è³ããããšãã§ããŸã-圌ãã¯äž¡æ¹ã®ã«ãŒã«ã«åŸã£ãŠãéåžžã«è«ççã«è¡åããŸããïŒPDO :: quoteïŒïŒé¢æ°ã¯äœæ¥ã®ååã§ã¯ãªããå šäœ-æååããšã¹ã±ãŒãããåŒçšç¬Šã§å²ã¿ãŸããåãããšãè¡ããŸãã
function escapeString($value) { return "'".mysqli_real_escape_string($this->connect,$value)."'"; }
ããšã³ã³ãŒãã£ã³ã°ãæ£ããèšå®ãããŠãããããšãã§ããã®ã¯ãé¢æ°mysqli_set_charset / mysql_set_charsetããã³PDO-DSN ã®ã¿ã§ãããšããããšãè¿œå ããã ãã§ãã
æ°å€ã®æžåŒèšå®
çè«çã«ã¯ãã»ãšãã©ã®å Žåãæ°åã¯æååãšããŠãã©ãŒãããã§ããŸãããã®åŸãã¿ã¹ã¯ã¯ä»¥åã®ãã®ã«åæžãããŸãããããã3ã€ã®åé¡ããããŸã
- mysqlã®STRICT MODEã¢ãŒããæå¹ã«ãããšãæååãæ°å€ãšããŠæž¡ãããšãããšãã«ãšã©ãŒãã¹ããŒãããå ŽåããããŸãã
- LIMITæŒç®åãæååã®äœ¿çšã¯ãŸã£ããæäŸãããŠããŸãã
- mysqlã®å°é家ã«ãããšããªãã©ã«ã®ã¿ã€ãã¯éåžžã«éèŠã§ãããã¯ãšãªã®èšç»ãšå®è¡ã«éèŠã§ãããšã³ã¡ã³ãããŠããŸããç§èªèº«ã¯ãããåŸæã§ã¯ãªãã®ã§ã詳现ãªã³ã¡ã³ããããã ããã°ããããã§ãã
å¥ã®åé¡ã¯ãPHPã®çµã¿èŸŒã¿åå€æã¡ã«ããºã ã®ããã深床ãäžååã§ããããšã§è¡šãããŸãããããã£ãŠãPHP_INT_MAXãã倧ããæŽæ°å€ã䜿çšããå¿ èŠãããå Žåã¯ãæ€èšŒã«æ£èŠè¡šçŸã䜿çšããŠãã ãããããŠãéåžžã®å ŽåãintvalïŒïŒã䜿çšã§ããŸãã
PHPã«ã¯MySQLã®éåžžã«äŸ¿å©ãªDECIMALåã«äŒŒãããŒã¿åããªããããå®æçãªééã§ã®ã¿å°æ°ç¹ä»ãã®æ°å€ããã§ãã¯ããããšããå§ãããŸãã
ã¬ã€ãžãŒãã¬ãŒã¹ãã«ããŒ
ããã§ã¯ãå®è£ ã«åãââããããŸãããã
æåã«æãæµ®ãã¶ãªãã·ã§ã³ã¯sprintfïŒïŒã§ããå®éããã®é¢æ°ãã¡ããªãŒã®ã¯ã€ã«ãã«ãŒãè¡šçŸã¯å®éã®ãã¬ãŒã¹ãã«ããŒã§ãããããŠtipizovannyeãã¬ãŒã¹ãã«ãïŒã€ãŸããããŒã¿ã®åŠçæ¹æ³ã¯ããã¬ãŒã¹ãã«ããŒèªäœã«ãã£ãŠæ±ºå®ãããŸããå®éãããã¯ããã»ã©éèŠãªããšã§ã¯ãããŸãããPDOã®äœè ãmysqliã®äœè ããã®ããã«èããŠããŸããããŸããç¹å¥ãªé¢æ°ãåŒã³åºãããšã«ãã£ãŠã®ã¿ããããã®ã©ã€ãã©ãªã§çœ®æãããããŒã¿ã®ã¿ã€ããæå®ã§ããŸãã
äžè¬ã«ãsprintfã¯æååãé€ããã¹ãŠã®ããŒã¿åãåŠçããŸãããã¡ãããsprintfã¯è¡ããã©ãŒãããããŸãããããã®ãŸãŸè¡šç€ºããŸããããããããã¯ç§ãã¡ã«ã¯é©ããŠããŸãã-å³æ Œãªãã©ãŒãããèŠåããããŸãããããŠãããã¯ã«ãŒã«ã ãšãã«ãŒã«ã§ã¯ãããŸãããç§ãã¡ãæãåºãããã«ãéããããšã¯æŠãã®ååã«éããŸããã
ããããç§ãã¡ã¯ããã°ã©ããŒã«åŒçšç¬Šãè¿œå ããããšã«ããã»ã©éèŠãªä»äºãäžããããšãã§ããªãã®ã§ãç§ãã¡ã®ã³ãŒãã¯ãããèªåã§ãããªããã°ãªããŸãããæããªæååã®çœ®æãããã¯çæ³çãªãœãªã¥ãŒã·ã§ã³ã§ã¯ãããŸãããããã¬ãŒãã³ã°ã³ãŒãã«é©ããŠããŸãã
ããã«ãé¢æ°ã«è»¢éããããã¹ãŠã®ããŒã¿ããšã¹ã±ãŒãããŸããã¯ããããã¯ééã£ãŠããŸããããããããã«ãã害ã¯ãããŸããããsprintfïŒïŒã®äž»ãªå©ç¹ãã€ãŸãæååã解æãããšããäºå®ãç¶æããŸãã
ãªã害ããªãã®ã§ããïŒçµå±ã®ãšãããç§ã¯ããªãã«ãã©ã€ã³ä»¥å€ã®ãã®ããšã¹ã±ãŒãããããšã¯åœ¹ã«ç«ããå±éºã§ãããšèšã£ãã ãã§ãïŒã¯ããæ®ãã®ããŒã¿ãåŠçããªããªã£ãå Žåããã ãããã¬ãŒã¹ãã«ããŒã®æ®ãã®ãªãã·ã§ã³ã¯æ°å€ã®ã¿ã§ãããããã€ã³ãžã§ã¯ã·ã§ã³ã¯ããããééããŸããã
ãã®çµæãé¢æ°ãåŸãŸãã
function query(){ $query = array_shift($args); $query = str_replace("%s","'%s'",$query); foreach ($args as $key => $val) { $args[$key] = mysql_real_escape_string($val); } $query = vsprintf($query, $args); if (!$query) return FALSE; $res = mysql_query($query) or trigger_error("db: ".mysql_error()." in ".$query); return $res; }
ãªã¯ãšã¹ãã«ããŒã¿ãä»£å ¥ãããšãã矩åã«ããŸã察åŠããŸãã
ããã«ãäžå šãæãããã«ïŒãã¬ãŒã¹ãã«ããŒãæ··åããããšãã°ãïŒ dã®ä»£ããã«LIMITæŒç®åã«ïŒ sãå ¥ãããšãéçºæ®µéã§ãªã¯ãšã¹ããšã©ãŒãçºçããŸãããã€ã³ãžã§ã¯ã·ã§ã³ã¯çºçããŸããïŒãšã¹ã±ãŒãã®å Žåã«çºçããŸãïŒã
ãã¡ãããprintfïŒïŒã«åºã¥ããå®è£ ã«ã¯æ¬ ç¹ããªãããã§ã¯ãããŸããããªã¯ãšã¹ãã«çŽæ¥æžã蟌ãŸãããããïŒã«å ããŠã3çš®é¡ã®ãã¬ãŒã¹ãã«ããŒã®ã¿ã確èªããå¿ èŠããããŸãã
ããããOOPãæ¬åœã«å«ããªäººã®ããã«ãããã§äœå¹Žãåã«æžããæ©èœã玹ä»ããŸããèšå®ãã¡ã€ã«ã«ã³ããŒããŠäœ¿çšãéå§ããã¯ãšãªãå®è¡ããå®å šãªæ¹æ³ãååŸããŸãã 4åã
ã³ãŒãã衚瀺
function dbget() { /* usage: dbget($mode, $query, $param1, $param2,...); $mode - "dimension" of result: 0 - resource 1 - scalar 2 - row 3 - array of rows */ $args = func_get_args(); if (count($args) < 2) { trigger_error("dbget: too few arguments"); return false; } $mode = array_shift($args); $query = array_shift($args); $query = str_replace("%s","'%s'",$query); foreach ($args as $key => $val) { $args[$key] = mysql_real_escape_string($val); } $query = vsprintf($query, $args); if (!$query) return false; $res = mysql_query($query); if (!$res) { trigger_error("dbget: ".mysql_error()." in ".$query); return false; } if ($mode === 0) return $res; if ($mode === 1) { if ($row = mysql_fetch_row($res)) return $row[0]; else return NULL; } $a = array(); if ($mode === 2) { if ($row = mysql_fetch_assoc($res)) return $row; } if ($mode === 3) { while($row = mysql_fetch_assoc($res)) $a[]=$row; } return $a; } ?>
䜿çšäŸïŒ
$name = dbget(1,"SELECT name FROM users WHERE id=%d",$_GET['id']); // $user = dbget(2,"SELECT * FROM users WHERE id=%d",$_GET['id']); // $sql = "SELECT * FROM news WHERE title LIKE %s LIMIT %d,%d"; $news = dbget(3,$sql,"%$_GET[search]%",$start,$per_page); //
ããã€ãã®åä»ãã«ãé¢ãããããã®é¢æ°ã¯ããç¥ãããŠããååã«å³å¯ã«æºæ ããŠæžãããŠãããããç§ã®å¿ã«æãããŠããŸããKISSã«èããããšãã§ããã°ããã®ã¢ããªã±ãŒã·ã§ã³ã¯ã³ãŒããéåžžã«ä¹Ÿç¥ãããŸãã
ããããç§ãã¡ã¯çããããè¡ãæ¢ãŸãã®ãã©ã³ãã§ããããšãç解ããŠããŸãããããŠãå®å šãªã¯ã©ã¹ãå¿ èŠã§ãã
æ®å¿µãªãããèšäºã®éã¯ãã§ã«ãã¹ãŠã®åŠ¥åœãªãµã€ãºãè¶ ããŠãããã¯ã©ã¹ã®äœæã¯å¥ã®æçš¿ã§è¡ãå¿ èŠããããŸããããã€ãã®è³ªåã ããåãäžããŸãã
ãŸããç§ãã¡ã®ã¯ã©ã¹ã¯äžèšã®æ©èœã®çžç¶äººãšãªããç¹°ãè¿ãã®ã³ãŒãããªãããšããé«ã䜿åœãæãããŸãããããã£ãŠãã¯ã©ã¹ã¯ããã¬ã€ãã«ããŒã®ãµããŒãã«å ããŠãããŒã¿ããŒã¹ããæ å ±ãç®çã®åœ¢åŒã§ããã«ååŸããããã®äžé£ã®ãã«ããŒé¢æ°ãæäŸããŸããã¹ã«ã©ãŒã1次å é åã2次å é åã1次å é åã®ãã£ãŒã«ãã«ãã£ãŠã€ã³ããã¯ã¹ä»ãã2次å é åã®ãã£ãŒã«ãã«ãã£ãŠã€ã³ããã¯ã¹ä»ã
第äºã«ãã¯ã©ã¹ã§ã¯ãåä»ããã¬ãŒã¹ãã«ããŒãšããæ¬åœã«çŽ æŽãããã¢ã€ãã¢ã䜿çšããŸããåæã«ãPDOãšããååã®ãã¬ãŒã¹ãã«ããŒããå©çãåŸãŸãã
ãã¬ãŒã¹ãã«ããŒã次ã®åœ¢åŒã«ããŸã
[az]:[az]*
äŸãã°
ãŸãã¯i:
s:name
æåã®ã±ãŒã¹ã§ã¯å¿åãã¬ãŒã¹ãã«ããŒã«ãªãã2çªç®ã®ã±ãŒã¹ã§ã¯ååä»ãã«ãªããŸãã
æåã®æåã¯ã¿ã€ããèšå®ããã³ãã³ã¯ãã¬ãŒã¹ãã«ããŒãè¡ã®ä»ã®èŠçŽ ãšåºå¥ããååã¯ãªãã·ã§ã³ã§ãã
第äžã«ãINïŒïŒæŒç®åïŒæååçšïŒã®åŸ æã®ãã¬ãŒã¹ãã«ããŒ
function createIN($data) { if (!is_array($data)) { throw new E_DB_MySQL_parser("Value for a: type placeholder should be array."); } if (!$data) { throw new E_DB_MySQL_parser("Empty array for a: type placeholder."); } $query = $comma = ''; foreach ($data as $key => $value) { $query .= $comma.$this->escapeString($value); $comma = ","; } return $query; }
ãããŠãããã«é¢ããããã€ãã®ã³ã¡ã³ãã
ã芧ã®ãšãããããŒãµãŒã¯ç©ºã®é åã«äŸå€ãã¹ããŒããŸãããªãã§ïŒ
mysqlã¯ç©ºã®INïŒïŒãèªãã®ã§ããšã«ãããªã¯ãšã¹ããå®è¡ããã®ã¯ç¡æå³ã§ãã
ã©ããã§ç Žè£é³ãšåŒçšç¬Šããšããžã®åšãã«ä»ããæ©ç¥ã«å¯ãã ããŒãžã§ã³ãèŠãŸããããããã¯ã§ããŸããã空ã®æååã¯æ£çŽãªå€ã§ãããé åã§æ瀺çã«æž¡ãããå Žåã«ã®ã¿æ¢ãã®ãçã«ããªã£ãŠããŸãã
dbSimpleã©ã€ãã©ãªã®DmitryKoterovã¯ããã®åé¡ãããªãããŸã解決ããŠããŸããINæŒç®åãå«ããããã¯å šäœãäžæ¬åŒ§ã§å²ãŸãã空ã®é åã®å Žåããªã¯ãšã¹ãããå®å šã«åé€ãããŸããç§ã¯ãã®æ±ºå®ã®æ£ããã確信ããŠããŸããããå°ãªããšãéåžžã«æ©ç¥ã«å¯ãã§ããŸãã
æŽæ°ïŒ
zerkmsãšdavid_mzããã¡ã€ã«ããããšããã¢ã€ãã¢ã®ãããã§ã空ã®é åã®åé¡ã¯éåžžã«ç°¡åã«è§£æ±ºãããããšãããããŸããïŒ
ãšã©ãŒãã¹ããŒãããåžžã«FALSE-空ã®é åã®çæ³çãªè¡šçŸãè¿ããŸããIN(NULL)
ã€ãŸãããããNULLã«çœ®ãæããã ãã§ãã
function createIN($data) { if (!is_array($data)) { throw new E_DB_MySQL_parser("Value for a: type placeholder should be array."); } if (!$data) { return 'NULL'; } $query = $comma = ''; foreach ($data as $key => $value) { $query .= $comma.$this->escapeString($value); $comma = ","; } return $query; }
ãã®ãããªèæ ®äºé ãããæåã®äŸå€ãæ®ãããšã«ããŸããã
çè«çã«ã¯ãã¹ã«ã©ãŒãé åã«å€æããéåžžã®æ¹æ³ã§ããã«åŠçããããšãã§ããŸãããããããã®ãããªé ãã¿ã€ãã®å€æã¯ãèšèªã«ã¯éŠŽæã¿ããããŸãããè«ççãªãšã©ãŒã«æºã¡ãŠããŸãã
ãã®çµæãé åãã³ãŒãå ã§åœ¢æãããå Žåããã®ãã§ãã¯ã¯éçºæ®µéã§åœ¢æã®ãã¹ãŠã®ãšã©ãŒããã£ããããã®ã«åœ¹ç«ã¡ãŸããããŠãé åãå€éšããæ¥ãå Žåãããã«äŸå€ãã¹ããŒããå¿ èŠããããŸãã
</ update>
ç§èªèº«ã¯ãã¢ã¬ã€ã®äºåæ€èšŒãè¡ã£ãããªã¢ã³ããäœæããŸãããå¥ã®ã¯ã©ã¹ã¡ãœãããããã«åœ¹ç«ã¡ãŸã-解æïŒïŒã¯ããã¬ãŒã¹ãã«ããŒã§æååã解æããæž¡ããããã©ã¡ãŒã¿ãŒã眮æããæ¢è£œã®SQLã¯ãšãªãçæããŸã...ãŸãã¯ãã®äžéšïŒçµå±ããµãŒããŒåŽã®ãã¬ãŒã¹ãã«ããŒã®å Žåãšã¯ç°ãªãããªã¯ãšã¹ãã®ä»»æã®éšåãç°¡åã«è§£æã§ããŸãïŒ ïŒãããŠã1ã€ç®ããããã°ã«åœ¹ç«ã€å Žåã2ã€ç®ã¯ç§ãã¡ã®ãããªå Žåã§ãã
if (is_array($array) and $array) { $sql .= $db->parse(" AND type IN(a:)",$array); }
ãŸãããã®ã¡ãœããã䜿çšããŠãè€æ°éã®æ¡ä»¶ä»ãWHEREãæ§æã§ããŸãã
$w = array(); $where = ''; if (!empty($_GET['type'])) $w[] = $db->parse("type = s:", $_GET['type']); if (!empty($_GET['rooms'])) $w[] = $db->parse("rooms IN (a:)",$_GET['rooms']); if (!empty($_GET['max_price'])) $w[] = $db->parse("price <= i:", $_GET['max_price']); if (count($w)) $where = "WHERE ".implode(' AND ',$w); $data = $db->getArr("SELECT * FROM table $where LIMIT i:,i:",$start,$per_page);
ã¡ãªã¿ã«ãåè¿°ã®DbSimpleã©ã€ãã©ãªã¯ãããã§æ瀺ãããŠããã»ãšãã©ã®ã¢ã€ãã¢ã®ç°¡åãªå®è£ ã®äŸãšããŠåå¥ã®æ®µèœãšããŠèšåãã䟡å€ããããŸãïŒç¹ã«ã100,500çš®é¡ä»¥äžã®ãã€ãã£ãã®åä»ããã¬ãŒã¹ãã«ããžã®èªå·±èšè¿°ãã¬ãŒã¹ãã«ãã®ç¿»èš³ïŒããã¶ã10幎åã§ãããäœããã®çç±ã§åºãæµéããŸããã§ããã
è¿·ä¿¡æŽé²ã»ãã·ã§ã³
誀解ã«ã€ããŠå°ã話ããŸãããã
ç§ã¯ãWebéçºã®ãã®ãããªãããã¯ã«ã€ããŠã¯ç¥ããŸãããããã¯ãåæ§ã®æ°ã®åŠæ³ãè¿·ä¿¡ã§å€§ãããªããããŠããã§ãããããããæšæºåã®ããŒãã§ããïŒ
ã©ããããããã§ã®ã¡ã«ããºã ã¯ããã§ãïŒæã ã¯éåžžã«æ©ã段éã§ãéåžžã«åæã®æ³šå°ã«å¯Ÿããä¿è·ã®åºæ¬ãç¥ãããã«ãªããŸãããããŠãåŠãã ã«ãŒã«ã¯ãæºãããªãçå®ã®åºç€ãšããŠèšæ¶ãããŠããŸãããããŠãä»åŸã¯ãã®åé¡ã®æ€èšã«æ»ããŸããããããŠããã¯å¿ èŠã§ãããã
å ããŠããã€ãã®ããã«ãã€ã³ã¿ãŒãããäžã«ã¯ã»ãšãã©æå³ã®ããæ å ±ã¯ãããŸããããæ inessã¯ãããèªåã§ãã¹ãããŠç解ããããšã§ãããã®çµæãæ¢åã®è¿·ä¿¡ã«å ããŠãæ°ãããããã»ã©ãããã§ãã·ã§ãã«ã§ã¯ãªãè¿·ä¿¡ãã€ã³ã¿ãŒãããã«æŸã蟌ãŸããŠããŸãïŒãããŠå€§è¡ã«åãäžããããŠããŸãïŒïŒãããã®ããã€ãã«å¯ŸåŠããŠã¿ãŸãããã
Mysql_ *é¢æ°ã¯PHPã§é·ããå»æ¢ãããŸãã
æ²ãããããã¯ã
æ lazãšç¡ç¥ãçŸããå³æžé€šãåããç©èªã
ãŸãããæ©èœãã§ã¯ãªããæ¡åŒµå šäœã§ãã
第äºã«ãéæšå¥šã§ã¯ãããŸããããèœèãããããŠãã£ãšåã§ã¯ãããŸããããæè¿ã§ã¯ãç ç³ã§ããããŸããããé åãããæ£ç¢ºã«ããå¿ èŠããããŸãã
第äžã«ããã®çŸããå®å®ããã©ã€ãã©ãªãæŸæ£ããæ¬åœã®çç±ã¯ãããŸããããããŠã圌ãã圌女ãèšèªããé€å€ãããå¯äžã®çç±ã¯ãããããããã¡ã³ãããããªãã£ãããã§ãããŸããããã«å ããŠã瀟äŒèªäœããããæ£ããåŠçããæ¹æ³ãåŠãã§ããªããšããäºå®ã«ãã£ãŠã®ã¿çºå±ããåŠå®çãªäžè«ããã ããã¹ã¯ã©ããã«å¯Ÿããè°è«ã¯ãããŸãããããã¥ã¢ã«ã®å€§ããªèµ€ãèŠåã¯ãmysqlæ¡åŒµæ©èœããç·æ¥ã«ç§»è¡ããå¿ èŠãããããšã瀺ããŠããŸãã
ãããããã®ãã¬ãŒãºã«ã¯ãã1ã€åŸ®åŠãªãã¥ã¢ã³ã¹ããããããã¯éåžžã«éèŠã§ããå€ãã®åæéçºè ãç解ããŠããªããã®ã®ã«ããŽãªãŒããã
ãmysql_ *é¢æ°ãã«ã€ããŠå ·äœçã«èª¬æããå Žåãã¢ããªã±ãŒã·ã§ã³ã³ãŒãã§ã¯å®éã«ãããã¹ãã§ã¯ãããŸããã mysqli_ *ãpdo_ *é¢æ°ããŸãã¯ãã®ä»ã®ãã¢APIåŒã³åºããããã§ã¯ãã³ãŒãã§ã¯ãªãã¢ããªã±ãŒã·ã§ã³ã³ãŒãã«ã€ããŠèª¬æããŠããããšã«æ³šæããŠãã ãããAPIé¢æ°ãžã®ãã¹ãŠã®åŒã³åºãã¯ã©ã€ãã©ãªã«ããã±ãŒãžåããå¿ èŠããããã¢ããªã±ãŒã·ã§ã³ã³ãŒãã§ã¯ã©ã€ãã©ãªé¢æ°ã«ã¢ã¯ã»ã¹ããå¿ èŠããããŸãããããŠãããããã¹ãŠã®mysqlïŒiïŒ_queryïŒïŒãããã³mysqlïŒiïŒ_fetch_arrayïŒïŒã䜿çšããŠç¹å®ã®ã¯ãšãªãå®è¡ãããšãæ®å¿µãªããããæèã倱ãããŸãã
ãã®çç±ã¯åºæ¬çã«éåžžã«åçŽã§ãïŒAPIé¢æ°ã¯éåžžã«åé·ã§ããããŒã¿ããŒã¹ããããŒã¿ã®è¡ãååŸããã«ã¯ã1ããŒã¹è¡ãæžã蟌ãå¿ èŠããããŸãããããŠãããã¯ããšã©ãŒåŠçããã®ã³ã°ããªã¯ãšã¹ãã®ãããã¡ã€ãªã³ã°ãªã©ãå¿ èŠãªãã®ãèæ ®ããŠããŸããããã®çµæãAPIé¢æ°ãã³ãŒãã§çŽæ¥äœ¿çšãããšãäžæ¹ã§ã¯ã²ã©ãåé·ã«ãªããä»æ¹ã§ã¯ååã«æ©èœããªããªããŸãã
PDOäœæè ã¯ã©ã€ãã©ãªããã䜿ããããããããšããŸããããç§ã®æèŠã§ã¯ãååã§ã¯ãããŸããããŸããPDOãžã®åŒã³åºãã¯ãç¬èªã®ã¯ã©ã¹ã®ã¡ãœããã§åãæ¹æ³ã§ã«ãã»ã«åããå¿ èŠããããŸãã
å¥ã®åŒæ°-ããã§ãmysql_ *ãå¥ã®ãã®ã«å€æŽããå¿ èŠããããŸããåäžã®ã©ã€ãã©ãªãã¡ã€ã«ã§ã¯ãããã¯æ°åã®ãããžã§ã¯ããããã¯ããã«ç°¡åã§ãã
ç¹å¹è¬ã¯ãããŸãã
ã芧ã®ãšããããããŸããå®è£ ãšåŸããããéåžžã«ã·ã³ãã«ãªã«ãŒã«ã®ã»ããã
ç§ãããã«æåããªãã£ããšèšããªããã°ãªããŸãããæåã¯ãªã¹ãã¯ç¶²çŸ çã§ã¯ãªãããã®åŸã³ã³ãã¯ãã§ããã
éãã
è¿·ä¿¡ããããããããŸããäžèšã®èª¬æã®åŸã質åãæ®ã£ãŠããªãããšãé¡ã£ãŠããŸããã念ã®ããïŒ
- ãmysql_real_escape_stringé¢æ°ã®åé¡ã¯ããŸã£ãã䜿çšãããªãããšã§ãã-ããã¯ãã³ã»ã³ã¹ã§ããmysql_real_escape_stringïŒïŒé¢æ°ã®å¯äžã®åé¡ã¯ãéåžžã«å€ãã®äººã ããããäœã®ããã«ããã®ãç解ããŠããªãããšã§ãã
- ãåä¿¡ããŒã¿ã«ã¯ãšã¹ã±ãŒããå¿ èŠã§ãã-ãã¡ãããããã§ã¯ãããŸãããæååã«ã¯ãšã¹ã±ãŒããå¿ èŠã§ã
- « ââ ââ» â . «» . , , , , â
- « - » â , ? . , .
- « » â , , . , â . â , SQL . PDO, â , , . PDO . , , «» . PDO «», .
«»
ãUNIONããäžéåŒçšç¬Šãªã©ãä¿è·ã®éåžžã«é¢çœãæ¹æ³ãèè ã®ãµã€ãã§å¥ã®ããŒãžã§ã³ãçå£ã«æäŸããŠããã®ãèŠããšãããµã€ãèªäœãä¿è·ãããŠããå Žåã¯ã©ããªãããåžžã«å°ãããã®ã§ãããç§ãã¡ã®æ²æšãªçºæè ã¯ããããã®éåžžã«ãå±éºãªããã¹ãããã¯ãŒãããå«ãæçš¿ãå ¬éã§ããŸããã§ããã
ãŠãããŒãµã«ããŒã¿ä¿è·æ©èœ
ãŸããåå¿è ã®phpshniksã®éã§ã¯éåžžã«ãããã誀解ã§ãã ãç§ãã¡ã¯ãå ¥åããŒã¿ã®ãã¹ãŠã®ãæ害ãªæåãã殺ãããšã§ãã€ã³ãžã§ã¯ã·ã§ã³ãã身ãå®ãæ±çšæ©èœãå ¥åã«å ¥ããŸããïŒã
ãããããã§ã«ããã£ãŠããããã«ããæ害ãªããã£ã©ã¯ã¿ãŒã¯ååšããŸãããç¹å®ã®ã³ã³ããã¹ãã§ã®ã¿ç¹å®ã®æå³ãæã€ãµãŒãã¹ãã£ã©ã¯ã¿ãŒã®ã¿ãååšããŸãããã以å€ã®å Žåã¯å®å šã«å®å šã§ãã
ãŸãããŸãã»ãã¥ãªãã£ãå¿ èŠã§ã¯ãªããããŒã¿ã®æ£ãããã©ãŒããããå¿ èŠã§ããããšãããããŸããããããŠãããã¯ãã©ã®ã³ã³ããã¹ãã§äœ¿çšãããããããã£ãŠããå Žåã«ã®ã¿å®è¡ã§ããŸããæ®å¿µãªãããã¹ã¯ãªããå ¥åã§ã¯ãŸã ãããç¥ããŸããããã®ã¢ãããŒãã®é®®æãªäŸã¯ãæªåé«ãmagic_quotesãã£ã¬ã¯ãã£ãã§ããããã¯å¹žããªããšã«ãèšèªããåé€ãããŠããŸãããŸã£ããå¿ èŠã®ãªãããŒã¿ãåŠçããããšã§ãã»ãã¥ãªãã£ã®å¹»æ³ãäœãåºããã©ã€ã³ã®ã¿ãä¿è·ããŸãïŒãããŠãå€éšããã¹ã¯ãªããã«ã®ã¿é¥ããŸãïŒã
ãã®ããããããã®ãšã©ãŒãç¹°ãè¿ããªãã§ãã ãããã¹ã¯ãªããã«å ¥ã£ãçŽåŸã«ããŒã¿ãSQLçšã«ãã©ãŒãããããªãã§ãã ããã
ãšã³ã³ãŒãã£ã³ã°
. .
, « » , mysql_real_escape_string() (!)
, , mysql_real_escape_string() « », . â latin1, , mysql_real_escape_string() mysql_escape_string().
åæã«ã2006幎ã«ã¯ãmysql_real_escape_stringïŒïŒé¢æ°ã«mysqlæ¡åŒµæ©èœã®çŸåšã®ãšã³ã³ãŒãã£ã³ã°ãäŒããæ¹æ³ããããŸããã§ããããããããã以æ¥ãé²æ©ã¯éåžžã«é²ã¿ãããŒã«ãç»å ŽããŸãã-mysql_set_charsetïŒïŒé¢æ°ã§ããããã¯ãSET NAMESèŠæ±ã®ä»£ããã«ã¯ã©ã€ã¢ã³ããšã³ã³ãŒãã£ã³ã°ãèšå®ããããã«äœ¿çšããå¿ èŠããããŸãã
ãããã«ãããæ³šå ¥ã¯äžéšã®ãšããŸããã¯ãªãšã³ã³ãŒãã£ã³ã°ã«å¯ŸããŠã®ã¿å¯èœã§ãããã¹ãŠã®ã·ã³ã°ã«ãã€ããšã³ã³ãŒãã£ã³ã°ãšUTF-8ã¯å®å šã§ãã®ã§ïŒãã®çµæãæªåé«ãaddslashesïŒïŒããããã«é©ããŠãããšããäºå®ã§ãïŒã
åœæãç§ã¯ããããã¹ãŠã©ã®ããã«æ©èœãããããã§ãã¯ããã®ãé¢åã§ã¯ãããŸããã§ãããç§ã¯ç¢ºä¿¡ããŠããŸãã-äºæž¬ã©ããã«æ©èœããŸãã
ãã¿ãã¬ã®äžã§ãèŠãã®ãé¢åã§ã¯ãªãå Žåããã¹ãçµæã
MySQL.
Php
çµæ
mysql> select version(); +---------------------+ | version() | +---------------------+ | 5.0.45-community-nt | +---------------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE users ( -> username VARCHAR(32) CHARACTER SET GBK, -> password VARCHAR(32) CHARACTER SET GBK, -> PRIMARY KEY (username) -> ); Query OK, 0 rows affected (0.08 sec) mysql> insert into users SET username='ewrfg', password='wer44'; Query OK, 1 row affected (0.02 sec) mysql> insert into users SET username='ewrfg2', password='wer443'; Query OK, 1 row affected (0.03 sec) mysql> insert into users SET username='ewrfg4', password='wer4434'; Query OK, 1 row affected (0.00 sec)
Php
<pre><?php echo "PHP version: ".PHP_VERSION."\n"; mysql_connect(); mysql_select_db("test"); mysql_query("SET NAMES GBK"); $_POST['username'] = chr(0xbf).chr(0x27).' OR username = username /*'; $_POST['password'] = 'guess'; $username = addslashes($_POST['username']); $password = addslashes($_POST['password']); $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($sql) or trigger_error(mysql_error().$sql); var_dump($username); var_dump(mysql_num_rows($result)); var_dump(mysql_client_encoding()); $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($sql) or trigger_error(mysql_error().$sql); var_dump($username); var_dump(mysql_num_rows($result)); var_dump(mysql_client_encoding()); mysql_set_charset("GBK"); $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'"; $result = mysql_query($sql) or trigger_error(mysql_error().$sql); var_dump($username); var_dump(mysql_num_rows($result)); var_dump(mysql_client_encoding());
çµæ
PHP version: 5.3.3 string(29) "Ñ\' OR username = username /*" int(3) string(6) "latin1" string(29) "Ñ\' OR username = username /*" int(3) string(6) "latin1" string(30) "\Ñ\' OR username = username /*" int(0) string(3) "gbk"
å ã»ã©è¿°ã¹ãç¹åŸŽçãªè©³çŽ°ïŒ
æè¿ãŸã§ãPDOã§ã¯æ¥ç¶ã®ãšã³ã³ãŒãããŸã£ããèšå®ã§ããŸããã§ãããPDOã«ã¯mysql_set_charsetïŒïŒã«äŒŒãé¢æ°ã¯ãªããããŒãžã§ã³5.3ããåã®DSNã«ã¯charsetãã©ã¡ãŒã¿ãŒã®ãããŒã®ã¿ãããããšã©ãŒãçæããããšã³ã³ãŒããå ¬éããŸããã§ããã
æ¬è³ªçã«ç¹å¥ãªããšã¯äœããããŸãããPDOããã¹ãŠã«å¯ŸããŠä¿è·ããæ¹æ³ã«ã€ããŠè©±ããã£ã©ã¯ã¿ãŒããããŒã«ããèœåãé€ããŠã
ãLIKEã®è匱æ§ã
LIKEã«ã¯è匱æ§ã1ã€ãããããŸããããã®æŒç®åãä»ã®ç®çã«äœ¿çšããããšã§ãã
äœããã®çç±ã§ãèšäºã®ãã¹ãŠã®çš®é¡ã®ã³ã³ãã€ã©ãŒã¯ãè¡šçŸã«ã¡ã¿ãã£ã©ã¯ã¿ãŒãä»£å ¥ããå¯èœæ§ãããåå¿è ãæããããã®ã倧奜ãã§ããåæã«ãLIKEã䜿çšãããŠããå Žåãã¡ã¿æåã¯ã§ããå¿ èŠãããããšã誰ãæžããŸãããããã§ãªããã°ãLIKEã¯æå³ããªããŸãããã€ãŸãããã®è匱æ§ã«å¯ŸåŠããæ£ããæ¹æ³ã¯ãïŒ æåãš_æåããšã¹ã±ãŒãããããšã§ã¯ãªããããšãã°ãã¹ã¯ãŒãæ€èšŒãªã©ãå®å šäžèŽã«é¢å¿ãããå Žåã¯ãã®é¢æ°ã䜿çšããªãããšã§ãã以äžã§ãã
ãããã«
ãã®èšäºã®ç®çã¯mysqlæ¡åŒµæ©èœã修埩ããããšã§ã¯ãããŸããããããããããšã§ãããåé¡ã¯ããŒã«ã§ã¯ãªãããããä¿æããæã«ããããšã«æ³šæããŠãã ããããããŠããããã®éæã®ããã®ç®æšãšã¡ã«ããºã ã®ç解ãããã°ããæ代é ãã®ãããŒã«ã§ãããªãã¯ããæªãçµæãåŸãããšãã§ããŸãã
ãããã£ãŠãç§ã¯å¥ã®ä»£ããã«1ã€ã®ãå®æããæ€ãä»ããããšãããå€ãæ¹æ³ã眮ãæããæ°ããæ¹æ³ã宣äŒããŸããããç§ãã¡ãéæãããç®æšãçç±ãããã³ãããéæããããã®ãªãã·ã§ã³ã説æããããšããŸããã
ãããŠãå°ãªããšãå°ãæåããã°ãã¿ã¹ã¯ãå®äºãããšèããŸãã
æŽæ°ïŒ mysqlã§ã®äŸ¿å©ãªäœæ¥ãšSQLã€ã³ãžã§ã¯ã·ã§ã³ã«å¯Ÿããä¿è·ã®ããã®ã¯ã©ã¹ã§ãèšäºã§æŠèª¬ãããŠããååãå®è£ ããŸãã