ãã®èšäºã®å¯Ÿè±¡è ãã®èšäºã®äž»ãªç®çã¯ãmysqliãæ¢ã«äœ¿çšããããæ·±ãæãäžããããã§ã¯ãªãããããã«ã©ãããŒãæžããŠå ã®æ§æãå¿ãã人ã®ããã«ããã¹ãŠãæ£ã«çœ®ããããšã§ããã æ£èŠåãç¥ããªã人ã«ãã£ãŠèšèšããã倧èŠæš¡ã§éåžžã«å€ãããŒã¿ããŒã¹ãããæ§é ãå€§å¹ ã«å€æŽãããæ°ããããŒã¿ããŒã¹ã«ããŒã¿ã転éãããšãã«ééãããã¥ã¢ã³ã¹ãæ確ã«ããããšããŸããã
å€ãmysqlæ¡åŒµæ©èœããŸã 䜿çšããŠããŠãPDOãŸãã¯MySqliã«åãæ¿ããããšã®ã¿ãèããŠãã人ã«ãã®èšäºãèªãããšã¯å¯èœã§ããïŒ ãããå¿ èŠã ãšæããŸãã
MySqliãŸãã¯PDO
è¿å¹Žãç§ã¯ãã¬ãŒã ã¯ãŒã¯å°çšã®Webãµã€ããäœæããŸãããããã«ãããããŒã¿ããŒã¹ãçŽæ¥æäœããå¿ èŠããªããªããŸããã ãã°ããåãç§ã¯çŽç²ãªphpã§ãµã€ãã®äœæ¥ãéå§ããå€ãMySQL PHPæ¡åŒµæ©èœã®äœ¿çšã¯æšå¥šãããŠããŸããã
MySqliãšPDOãéžæããå¿ èŠããããŸããã ããã»ã©é·ããªãç 究ã®åŸãMySqliã«ç«ã¡å¯ãããšã«æ±ºããŸãããããã¯ãåœæç§ã«ã¯æãããããã«ãMySQLãæŸæ£ããŠä»ã®äœããæ¯æããæ¹æ³ããªãã£ãããšãé€ããŠãPDOãšãŸã£ããåãã ã£ãããã§ãã 以äžã«æžããŠããããã«ãããã¯å®å šã«çå®ã§ã¯ãããŸããããå°ãªããšã1ã€ã®é¡èãªéãããããŸãã
MySqliã¯ãPHPéçºè èªèº«ã䜿çšããããšããå§ãããŸã[ 1 ]
OOPãšæç¶ãåã€ã³ã¿ãŒãã§ãŒã¹
MySqliã䜿çšãããšãOOPã¹ã¿ã€ã«ãšæç¶ãã¹ã¿ã€ã«ã®äž¡æ¹ã§ã³ãŒããèšè¿°ã§ããŸãã OOPã¯ã»ãšãã©ã®habrã³ãã¥ããã£ã«é¢ããŠç§ã«è¿ãã®ã§ããã®èšäºã§äœ¿çšããŸãã
3ã€ã®äž»èŠãªã¯ã©ã¹
MySqliã«ã¯3ã€ã®äž»èŠãªã¯ã©ã¹ãããããããã«ã€ããŠã¯ãã®èšäºã§è©³ãã説æããŸãã
- mysqli-ããŒã¿ããŒã¹ãžã®æ¥ç¶ã確ç«ããããã«å¿ èŠã§ãå€ãMySQLæ¡åŒµã§è¡ã£ãã®ãšåãæ¹æ³ã§ã¯ãšãªãå®è¡ããå Žåã«åœ¹ç«ã¡ãŸãã
- mysqli_stmt-æ°ããMySqliæ©èœã䜿çšããããã«å¿ èŠã§ããæºåãããåŒã«å¯ŸããŠã¯ãšãªãå®è¡ããŸãã
- mysqli_result-é¢æ°ãçµã¿åãããŠãmysqliãŸãã¯mysqli_stmtã䜿çšããŠäœæãããã¯ãšãªã®çµæãååŸããŸãã
以äžã§ãããããã«ã€ããŠè©³ããèŠãŠãããŸãããã
DBæ¥ç¶
2ã€ã®æ¹æ³ããããŸãã
æåã®æ¹æ³ã æ¥ç¶ãäœæããå¿ èŠãããå Žåã
$ mysqli = new mysqli ïŒ 'localhost' ã 'my_user' ã 'my_password' ã 'my_db' ïŒ ;
if ïŒ $ mysqli- > connect_error ïŒ {
die ïŒ 'Connect ErrorïŒ' ã $ mysqli- > connect_errno ã 'ïŒ' ã $ mysqli- > connect_error ïŒ ;
}
2çªç®ã®æ¹æ³ã æ¥ç¶ãªãã·ã§ã³ã䜿çšããå¿ èŠãããå Žåã
$ mysqli = mysqli_init ïŒ ïŒ ;
if ïŒ ïŒ $ mysqli ïŒ {
die ïŒ 'mysqli_init failed' ïŒ ;
}
if ïŒ ïŒ $ mysqli- > options ïŒ MYSQLI_INIT_COMMAND ã 'SET AUTOCOMMIT = 0' ïŒ ïŒïŒ {
die ïŒ ãMYSQLI_INIT_COMMANDã®èšå®ã«å€±æããŸããã ïŒ ;
}
if ïŒ ïŒ $ mysqli- > real_connect ïŒ 'localhost' ã 'my_user' ã 'my_password' ã 'my_db' ïŒ ïŒ {
die ïŒ 'Connect ErrorïŒ' ã $ mysqli- > connect_errno ã 'ïŒ' ã $ mysqli- > connect_error ïŒ ;
}
$ mysqli-> connect_errnoããã³$ mysqli-> connect_errorã䜿çšããŠãæ¥ç¶äžã«çºçãã説æãšãšã©ãŒã³ãŒããååŸããŸãã æ°ããmysqliïŒïŒãš$ mysqli-> real_connectïŒïŒã®äž¡æ¹ãããšã©ãŒã®æ¥ç¶æã«PHPèŠåãšã©ãŒãåŒãèµ·ãããŸãã ãããã£ãŠãäžèšã®é¢æ°ã䜿çšãããšã©ãŒåºåã¯ãå®çšŒåãµãŒããŒãªã©ã§PHPãšã©ãŒè¡šç€ºãç¡å¹ã«ããŠããå ŽåããŸãã¯äœããã®æ¹æ³ã§ãã®ããŒã¿ãåŠçããå¿ èŠãããå Žåã«æå³ããããŸãã ãšã©ãŒã®å Žåã«ãã¹ãŠã®MySQLié¢æ°ãPHPèŠåãåŒã³åºãããã§ã¯ãªãããšã©ãŒãçºçããããšã確èªããããã«ãç¹å¥ãªé¢æ°ãåç §ããå¿ èŠããããããããã§ããã«ã€ããŠèšåããŸããã
æ¥ç¶äžã«ååŸãããªããžã§ã¯ãã¯ãå°æ¥äœ¿çšããããã«$ mysqliå€æ°ã«å²ãåœãŠãããŸããã ããã¯OOPã¹ã¿ã€ã«ã§ã¯æããã§ãããæç¶ãåã¹ã¿ã€ã«ã§ã¯ãã®ãªããžã§ã¯ããå¿ èŠã§ããããã¯ãmysqlé¢æ°ã䜿çšãããã³ã«æ¥ç¶åç §ãæž¡ãå¿ èŠããªãã£ãã¬ã¬ã·ãŒMySQLæ¡åŒµãšã¯å¯Ÿç §çã§ãã
ãããã¡ä»ãããã³ãããã¡ãªãã®çµæ
ããã«èª¬æããåã«ãããã2ã€ã®ã¿ã€ãã®çµæã®éãã«ã€ããŠèª¬æããããšæããŸãã
ãããã¡ãªãã®çµæãèæ ®ããŠãã ããã ãã®å ŽåãmysqlãµãŒããŒãçµæãå®å šã«åä¿¡ãããŸã§åŸ ããã«çµæã®èªã¿åããéå§ã§ããŸãã
å©ç¹ïŒ
- çµæãããæ©ãèªã¿å§ããããšãã§ããåŸ ã¡æéãççž®ãããŸãã
- çµæã¯ãRAMã®ã¹ããŒã¹ãå æããŸããã
çæïŒ
- åä¿¡ããè¡æ°ãç¥ãããšã¯ã§ããŸããã
- ç¹å®ã®çµæã«ç§»åããããšã¯ã§ããŸãããã€ãŸããæåããé çªã«ã®ã¿ããŒã¿ãèªã¿åãããšãã§ããŸãã
- ãã®çµæãã¯ããŒãºããããŸã§ãä»ã®ãªã¯ãšã¹ããå®è¡ããããšã¯ã§ããŸããã
ãããã¡ãªã³ã°ãããçµæã«ã¯ããããã®æ¬ ç¹ããªãããããªã¹ããããŠããå©ç¹ããããŸããã
ã¯ã©ã·ãã¯ã¯ãšãª
MySqliã¯ããå€å žçãªãã¯ãšãªã®å¯èœæ§ãæ®ããŸããããŠãŒã¶ãŒããéä¿¡ãããã¯ãšãªã®ã»ãã¥ãªãã£ã«ç¬èªã«é¢äžããããæåŸ ãããå Žåãã¬ã¬ã·ãŒMySQLæ¡åŒµã®å Žåãšåæ§ã§ãã ãããè¡ãã«ã¯ãé¢æ°$ mysqli-> real_escape_stringïŒïŒã䜿çšããããšããå§ãããŸãããã®é¢æ°ã§ã¯ãã¯ãšãªã«é 眮ããåã«ãã¹ãŠã®ããŒã¿ãåŠçããå¿ èŠããããŸãã
æ¥ç¶ã®å Žåãšåæ§ã«ããã®ãããªãªã¯ãšã¹ããçãããæ¹æ³ãšé·ãããæ¹æ³ã®2ã€ããããŸãã
çãã
$ result = $ mysqli- > query ïŒ 'query text' ã MYSQLI_USE_RESULT ïŒ ;
å¯èœãªå®æ°ïŒ
MYSQLI_STORE_RESULT-ãããã¡ãªã³ã°ãããçµæãããã©ã«ãå€ãè¿ããŸã
MYSQLI_USE_RESULT-ãããã¡ãªã
é·ãã
$ mysqli- > real_query ïŒ 'query text' ïŒ ;
echo ïŒ $ mysqli- > field_count ïŒ ; //çµæãšããŠåã®æ°ãè¿ããŸã
//çµæãåãåãåã«ååŸã§ãããããæè»æ§ãåäžããŸã
//çãã¡ãœãããšæ¯èŒããŠããã¡ãããåŒã³åºãå¿ èŠã¯ãããŸãã
$ result = $ mysqli- > use_result ïŒ ïŒ ; //ãããã¡ãªãã®çµæãè¿ããŸã
//ãŸãã¯
$ result = $ mysqli- > store_result ïŒ ïŒ ; //ãããã¡ãããçµæãè¿ããŸã
é¢æ°$ mysqli-> use_resultïŒïŒãŸãã¯$ mysqli-> store_resultïŒïŒã¯ãè€æ°ã¯ãšãªïŒè€æ°ã®ã¯ãšãªã§æ§æãããã¯ãšãªïŒã«ã䜿çšãããŸãã ãã®èšäºã®è€æ°ã®ãªã¯ãšã¹ãã¯èæ ®ãããŸããã
ãããã®æ§æã¯äž¡æ¹ãšãmysqli_resultãªããžã§ã¯ãã®åœ¢åŒã§çµæãè¿ããŸããããã¯ããããã¡ä»ãããã³ãããã¡ãªãã®äž¡æ¹ã§çµæãæäœããããã®äŸ¿å©ãªã€ã³ã¿ãŒãã§ã€ã¹ã§ãã
äžèšã§æžããããã«ããã¹ãŠã®MySQLié¢æ°ãPHPãšã©ãŒãã¹ããŒãããšã¯éããŸããã ãªã¯ãšã¹ããæ£ãããªãããµãŒããŒããšã©ãŒãè¿ããå ŽåãPHPã¯ãŠãŒã¶ãŒã«éç¥ããŸããã 確èªããã«ã¯ã次ã®æ©èœã䜿çšããŸãã
- $ mysqli->ãšã©ãŒ - ãšã©ãŒã®èª¬æ
- $ mysqli-> errno-ãšã©ãŒã³ãŒã
$ city = $ mysqli- > real_escape_string ïŒ $ city ïŒ ;
$ mysqli- > query ïŒ "SELECT * FROM` city` WHERE` id` = ' $ city ' " ïŒ ;
if ïŒ $ mysqli- > errno ïŒ {
die ïŒ 'Select ErrorïŒ' ã $ mysqli- > errno ã 'ïŒ' ã $ mysqli- > error ïŒ ;
}
ãã¯ã©ã·ãã¯ãã¯ãšãªæ§æã®å©ç¹ïŒ
- æºåãããåŒïŒ mysqli_stmtã¯ã©ã¹ïŒãããã¯ããã«ã³ã³ãã¯ãã§ãã
- ããã«ããããããã¡ãªã³ã°ãããŠããªãçµæãmysqli_resultã®åœ¢åŒã§ååŸã§ããŸãã æºåãããåŒã䜿çšãããšããããã¡ãªã³ã°ãããmysqli_resultã®çµæã®ã¿ãååŸã§ããŸãããŸãããããã¡ãªã³ã°ãããŠããªãmysqli_stmtããŒã«ã䜿çšããå¿ èŠããããŸããããã¯mysqli_resultãããæè»æ§ãå£ããŸãã
ç§ãèŠããå€å žçãªãã¯ãšãªæ§æã®å®çšçãªã¢ããªã±ãŒã·ã§ã³ïŒ
- ãããã¡ãªã³ã°ãããŠããªãã¯ãšãªã§ã mysqli_resultã§ã®äœæ¥ãå©çšãããå Žåã
- INSERTãUPDATEãREPLACEããŸãã¯DELETEã¯ãšãªã§ãããçãæ§æã䜿çšããå Žåã
ãã®ãããªã¯ãšãªã§ã¯ã次ã®ããããã£ã圹ç«ã¡ãŸãã
- $ mysqli-> Influence_rows-åã®ééžæã¯ãšãªã®åœ±é¿ãåããè¡ã®æ°
- $ mysqli-> insert_idã¯ãæåŸã®æ¿å ¥èŠæ±ã«å¯ŸããŠèªåçã«çæãããIDã§ãã
æºåããããªã¯ãšã¹ã
ãã¯ã©ã·ãã¯ãã«å¯Ÿããæºåæžã¿ã¯ãšãªã®å©ç¹ïŒ
- ããŒã¿ã®ã¿ãå€æŽãããäžé£ã®ãªã¯ãšã¹ãã§ã¯ããªã¯ãšã¹ãã¯äžåºŠã ããµãŒããŒã«éä¿¡ãããå°æ¥çã«ã¯å€æŽãããããŒã¿ã®ã¿ãéä¿¡ãããŸãã
- SQLã€ã³ãžã§ã¯ã·ã§ã³ã«å¯Ÿããä¿è·ã
mysqli_stmtã¯ã©ã¹ã¯ãMySQLiã§æºåãããã¯ãšãªãæäœããŸãã
æºåæžã¿ã¯ãšãªãäœæãã2ã€ã®æ¹æ³ã
//æåã®æ¹æ³-mysqliãªããžã§ã¯ãã䜿çš
$ mysqli- > prepare ïŒ "SELECT * FROM` sk2_articles` WHERE` id` =ïŒ " ïŒ ;
if ïŒ $ mysqli- > errno ïŒ {
die ïŒ 'Select ErrorïŒ' ã $ mysqli- > errno ã 'ïŒ' ã $ mysqli- > error ïŒ ;
}
// 2çªç®ã®æ¹æ³-mysqli_stmtãªããžã§ã¯ãã䜿çš
$ stmt = $ mysqli- > stmt_init ïŒ ïŒ ;
$ stmt- > prepare ïŒ "SELECT * FROM` sk2_articles` WHERE` id` =ïŒ " ïŒ ;
if ïŒ $ stmt- > errno ïŒ {
die ïŒ 'Select ErrorïŒ' ã $ stmt- > errno ã 'ïŒ' ã $ stmt- > error ïŒ ;
}
éãã¯ããšã©ãŒæ å ±é¢æ°ãåŒã³åºããããªããžã§ã¯ãã§ãã 2çªç®ã®æ¹æ³ã¯ããšã©ãŒãã§ãã¯ãä»ã®mysqli_stmté¢æ°ã䜿çšããŠifãããã¯ã«çµåã§ããããããã䟿å©ã«æããŸãã ãããè¡ãæ¹æ³ã¯ã以äžã®äŸã§èŠãããšãã§ããŸãã
$ id_min = 81 ;
$ id_max = 88 ;
$ stmt = $ mysqli- > stmt_init ïŒ ïŒ ;
if ïŒ
//ãªã¯ãšã¹ããæºåããŸããããŒã¿ãã©ãã«æ¿å ¥ããããã·ã³ãã«ã§ããŒã¯ããŸããïŒ ïŒãã¬ãŒã¹ãã«ããŒïŒ
ïŒ $ stmt- > prepare ïŒ "sk2_articlesããã¿ã€ãã«ãéžæWHERE id>ïŒand id <ïŒ" ïŒ === FALSE ïŒ
//å€æ°ããã¬ãŒã¹ãã«ããŒã«ãã€ã³ãããŸã
ãŸãã¯ïŒ $ stmt- > bind_param ïŒ 'ii' ã $ id_min ã $ id_max ïŒ === FALSE ïŒ
//çŸåšãã€ã³ããããŠããå€æ°ã«ããããŒã¿ãéä¿¡ããŸã
ãŸãã¯ïŒ $ stmt- > execute ïŒ ïŒ === FALSE ïŒ
//å€æ°ããã€ã³ãããŠãçµæãååŸããŸã
ãŸãã¯ïŒ $ stmt- > bind_result ïŒ $ title ïŒ === FALSE ïŒ
//ãªã¯ãšã¹ãããããã¡ãªã³ã°ãã
//ãã®è¡ãããã§ãªãå Žåããªã¯ãšã¹ãã¯ãããã¡ãªã³ã°ãããŸãã
ãŸãã¯ïŒ $ stmt- > store_result ïŒ ïŒ === FALSE ïŒ
//ãã€ã³ããããå€æ°ã§çµæãååŸããŸã
ãŸãã¯ïŒ $ stmt- > fetch ïŒ ïŒ === FALSE ïŒ
//æºåããããªã¯ãšã¹ããéããŸã
ãŸãã¯ïŒ $ stmt- > close ïŒ ïŒ === FALSE ïŒ
ïŒ {
die ïŒ 'Select ErrorïŒ' ã $ stmt- > errno ã 'ïŒ' ã $ stmt- > error ïŒ ;
}
echo $ title ;
äžèšã®ã³ãŒãã®ããã€ãã®èª¬æã
- $ stmt-> bind_paramïŒïŒã§ã¯ãæåã®ãã©ã¡ãŒã¿ãŒã¯ãã€ã³ãããããŒã¿ã®ã¿ã€ãã§ãïŒi-æŽæ°ãd-doubleãs-ã¹ããªã³ã°ïŒã 2çªç®ãš2çªç®ã¯ã $ stmt-> prepareïŒïŒã§æåãïŒãã§å®£èšããã察å¿ãããã¬ãŒã¹ãã«ããŒã«ãã€ã³ããããå€æ°ã§ãã ãããã¯$ stmt-> prepareïŒïŒã«ãªã¹ããããŠããé åºã§ã ããã¯ãPlesholdoryã«ååãä»ããããšãã§ããPDOãšã®é¡èãªéãã§ãããããçºè¡šã®é åºã«äŸåããŸããã
- $ stmt-> bind_resultã¯ãç§ã®æèŠã§ã¯ã mysqli_stmtã¯ã©ã¹ã®æã匱ãç¹ã§ããããã¯ããªã¯ãšã¹ãããã©ã®ããŒã¿ãåä¿¡ãããªã¯ãšã¹ãã§ã©ã®é åºã§å®£èšãããããç¥ãå¿ èŠãããããã§ãã ãSELECT * FROM ...ãã®å Žåãã»ãšãã©é©çšãããŸããã mysqli_resultã¯ã©ã¹ã¯ãçµæãåŠçããããã®ã¯ããã«æè»ãªã¢ãããŒããæäŸããŸãã å®å šã«å®¢èŠ³çã§ããããã«ã $ stmt-> result_metadataïŒïŒ é¢æ°ã䜿çšããŠãããŒã¿èªäœã®ãªãã¡ã¿ããŒã¿ã®ã¿ãå«ãmysqli_resultãååŸããåä¿¡ããåã確èªã§ããããšã«èšåãã䟡å€ããããŸãããäžæ£ç¢ºãªæ å ±ã®åçºãé¿ããããã
- $ stmt-> store_resultïŒïŒã¯ãè¡ã®ã³ã¡ã³ãã§æžããããã«ãªãã·ã§ã³ã§ããã次ã®ãããªé¢æ°ã䜿çšããããã«å¿
èŠã§ãã
- $ mysqli_stmt-> num_rows-çµæã®åä¿¡è¡æ°ã
- $ stmt-> data_seek-å éšãã€ã³ã¿ãæå®ãããçµææååã«ç§»åãã
- $ stmt-> free_resultïŒïŒ -ãŸãã $ stmt-> store_resultïŒïŒã䜿çšããå Žåã¯ã空ãã¡ã¢ãªãåŒã³åºãããšãå¿ããªãã§ãã ããã
- $ stmt-> fetchïŒïŒã¯ãããŒã¿ãæ£åžžã«/æ£åžžã«åä¿¡ãããªãã£ãå Žåã«TRUE / FALSEãè¿ããããŒã¿ããããªãå Žåã«NULLãè¿ããŸãã
- ifãããã¯ã§çµåããããã¹ãŠã®é¢æ°ããšã©ãŒã®å Žåã«$ stmt-> errorã«å ¥ããããã§ã¯ãªã ãäžéšã¯PHPèŠåãåŒã³åºããŸããã倱æããå Žåã¯ãã¹ãŠFALSEãè¿ãããããã®æ§é ã¯æåããããã§ãã $ stmt-> executeïŒïŒãŸãã¯$ stmt-> fetchïŒïŒãæ°åå®è¡ããå¿ èŠãããå Žåãããã¯æ©èœããŸããã
- èŠæ±ããããã¡ãªã³ã°ããããã©ããã«é¢ä¿ãªãã次ã®ããããã£ã䜿çšã§ããŸãã
- $ stmt-> field_count-çµæãšããŠã®åæ°ã
- $ stmt-> Influence_rows-åã®ãªã¯ãšã¹ãã®åœ±é¿ãåããè¡ã®æ°ã¯éžæã§ã¯ãããŸããã
- $ stmt-> insert_id-åã®æ¿å ¥ã®èªåçæãããIDã
mysqli_resultãªããžã§ã¯ãã®ã€ã³ã¹ã¿ã³ã¹ãšããŠçµæãååŸããããã«ã³ãŒããå€æŽããŸãã
$ id_min = 81 ;
$ id_max = 88 ;
$ stmt = $ mysqli- > stmt_init ïŒ ïŒ ;
if ïŒ
ïŒ $ stmt- > prepare ïŒ "ã¿ã€ãã«ãsx2_linksããéžæWHERE id>ïŒããã³id <ïŒ" ïŒ === FALSE ïŒ
ãŸãã¯ïŒ $ stmt- > bind_param ïŒ 'ii' ã $ id_min ã $ id_max ïŒ === FALSE ïŒ
ãŸãã¯ïŒ $ stmt- > execute ïŒ ïŒ === FALSE ïŒ
//ãããã¡ãªã³ã°ãããçµæãmysqli_resultã®åœ¢åŒã§ååŸãã
//ãããã¡ãªã³ã°ãããŠããªãçµæã¯ååŸã§ããŸãããããã«ã€ããŠã¯æ¬ ç¹ã§æžããŸãã
ãŸãã¯ïŒ ïŒ $ result = $ stmt- > get_result ïŒ ïŒ ïŒ === FALSE ïŒ
ãŸãã¯ïŒ $ stmt- > close ïŒ ïŒ === FALSE ïŒ
ïŒ {
die ïŒ 'Select ErrorïŒ' ã $ stmt- > errno ã 'ïŒ' ã $ stmt- > error ïŒ ;
}
$ row = $ result- > fetch_row ïŒ ïŒ ;
echo $ row [ 0 ] ;
mysqli_resultã¯ã©ã¹ãšããã䜿çšããçµæã®æäœ
äžèšã®ããã«ã mysqliã¯ã©ã¹ã䜿çšãããã¯ã©ã·ãã¯ãã¯ãšãªã䜿çšããŠmysqli_resultãªããžã§ã¯ããååŸã§ããŸãããã®åŸããããã¡ãªã³ã°ãŸãã¯ãããã¡ãªã³ã°è§£é€ãããã mysqli_stmtã¯ã©ã¹ã䜿çšããŠãããã¡ãªã³ã°ããŸãã ãã®ã¯ã©ã¹ã®é¢æ°ã®æ©èœã¯ãååŸããçµæã®çš®é¡ã«ãã£ãŠç°ãªããŸãããããã£ãŠãã¯ãšãªããããã¡ãªã³ã°ãããŠããªãå Žåãçµæå šäœãåŸãããªããããçµæãäœè¡ã§ããããç¥ãããšã¯ã§ããŸããã
ãã®ã¯ã©ã¹ã®é¢æ°ã»ããã¯ãå€ãæ¡åŒµæ©èœã§ã¯ããªãã¿ã®ããã§ãã
- $ result-> fetch_rowïŒïŒ -çµæã®çŸåšã®è¡ãçªå·ä»ãé åãšããŠååŸããŸãã
- $ result-> fetch_assocïŒïŒ -é£æ³é åãšããŠã
- $ result-> fetch_arrayïŒïŒ -é
åã¿ã€ãã¯å®æ°ã«ãã£ãŠèšå®ãããŸãã
- MYSQLI_ASSOC-é£æ³ã
- MYSQLI_NUM-çªå·ä»ãã
- MYSQLI_BOTH-äž¡æ¹ã
- $ result-> fetch_objectïŒïŒ -ãªããžã§ã¯ããšããŠã®çµææååã
$ result-> fetch_objectïŒïŒã«ã€ããŠåå¥ã«è©±ãããããšæããŸãã ãã®é¢æ°ã«ã¯2ã€ã®ãã©ã¡ãŒã¿ãŒããããäž¡æ¹ãšããªãã·ã§ã³ã§ãã
- class_name-ãªããžã§ã¯ãã®äœæã«åºã¥ããã¯ã©ã¹ã®ååã
- params-ãªããžã§ã¯ãã®äœææã«ã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ããããã©ã¡ãŒã¿ãŒã®é åã
ã芧ã®ããã«ãã³ã³ã¹ãã©ã¯ã¿ãŒãã¯ã©ã¹ã«æž¡ããšããã®é¢æ°ã䜿çšããçµæã®1è¡ã®é£æ³é åã倱æãããšããŸãã 圌女èªèº«ãããã£ãŒã«ãã®ååã«äžèŽããã¯ã©ã¹ã®ããããã£ãçµæã«å²ãåœãŠãŸãã ã¯ã©ã¹ããããã£ãèŠã€ãããªãå Žåããããªãã¯ã®ã¹ã³ãŒãã§åçã«äœæãããŸãã
ã¯ã©ã¹ããã¯
{
ãã©ã€ããŒã $ some1 ;
public $ some2 ;
ä¿è·ããã$ id ;
é¢æ° __construct ïŒ $ param1 ã $ param2 ïŒ {
$ this- > some1 = $ param1 ;
$ this- > some2 = $ param2 ;
}
}
$ book = $ result- > fetch_object ïŒ 'Book' ã array ïŒ 1ã2 ïŒ ïŒ ;
var_dump ïŒ $ book ïŒ ;
idãã£ãŒã«ãã1ã€ãããªãå Žåã«åºåãã
ãªããžã§ã¯ãïŒæ¬ïŒ [ 4 ]
private 'some1' => int 1
public 'some2' => int 2
ä¿è·ããã'id' => int 382
ãã®ã¯ã©ã¹ã®ä»ã®äŸ¿å©ãªæ©èœãšããããã£ïŒ
- $ result-> fetch_allïŒïŒ -ãã¹ãŠã®è¡ãçªå·ä»ã/é£æ³/äž¡æ¹ã®ã¿ã€ãã®é åãšããŠååŸãã
- $ result-> data_seekïŒïŒ -æå®ãããäžé£ã®çµæãžã®å éšãã€ã³ã¿ãŒã®èšå®ã
- $ result-> num_rows-çµæã®è¡æ°ã
åç §ïŒ
php.netã®å ¬åŒããã¥ã¡ã³ãã®MySQLiããŒãž
MySQLiã®ãã®ä»ã®äŸ
ãã·ã¢èªã®MySQLié¢æ°ã®ãªã¹ã