ãPHPãmysqliã¨ããªãã¢ãã¹ãã¼ãã¡ã³ãã«ã¤ãã¦
ããã«ã¡ã¯ã
ä»åã¯PHPã«ã¤ãã¦ã§ãã
æ®æ®µãã¬ã¼ã ã¯ã¼ã¯ã§æ¸ããã¨ãå¤ãã®ã§ããå¹³æã§ãããã¨ããã¨å¿ãã¦ãããã¨ãå¤ãã§ãã
ä»åã¯PHPããMySQLã®ä½¿ç¨ã«ã¤ãã¦ã§ãã
PHPãç¿ãå§ããããã¯mysql_connectã¨ã使ã£ã¦ããã§ãããç¾å¨ã§ã¯éæ¨å¥¨ã®ããã§ãã
MySQLã¸ã®æ¥ç¶ã¯ãmysqliãPDOã®ã©ã¡ãããæ¨å¥¨ããã¦ãã¾ãã
PDOããããè¯ããã§ãããä»åã¯é¦´æã¿ãããããªmysqliã«ã¤ãã¦æ¸ããã¨æãã¾ããï¼PDOã¯ãã¤ããã£ã¦ã¿ããã§ããï¼
mysqliã®åºæ¬çãªä½¿ãæ¹
mysqliã§ã¯ãªãã¸ã§ã¯ãåã¨æç¶ãåã®æ¸ãæ¹ãããã¾ããããã®è¨äºã§ã¯ãªãã¸ã§ã¯ãåã§æ¸ãã¾ãã
æç¶ãåã«ããæå³ããªãã¨æãã®ã§ã
以ä¸ã¯mysqliã®åºæ¬çãªã³ã¼ãã«ãªãã¾ãã
<?php $mysqli = new mysqli("host", "user", "password", "database"); if ($mysqli->connect_errno) echo "Failed to connect to MySQL: " . $mysqli->connect_error; $mysqli->set_charset("utf-8"); // æååãé²æ¢ $res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL"); // ã¯ã¨ãªã®çºè¡ $row = $res->fetch_assoc(); // çµæã®è¡ãé£æ³é åã§åå¾ echo $row['_msg']; $res->close(); // çµæã»ãããéæ¾ $mysqli->close(); ?>
åºæ¬çã«ã¯ãããªæãã§ãã
mysqlã®ã¨ãã¨ãã¾ãå¤ããã¾ãããã
注æç¹ã¨ãã¦ã¯ãæååãé²æ¢ã®ããã«UTF-8ãã»ãããããã¨ã¨ãçµæãåå¾ããã¯ã©ã¹ï¼mysqli_result)ãcloseãã¦ã¡ã¢ãªéæ¾ããã¹ãã¨ããç¹ã§ãã
fetch_assocã®ã¨ããã«ã¤ãã¦ã¯ããã使ããã®ãä¸ã«ã¾ã¨ãã¦ããã¾ããmysqlã¨ã¾ã£ããä¸ç·ã§ãããã
- mysqli_result::fetch_all
- çµæã®ãã¹ã¦ã®è¡ãé£æ³é åã»æ°å¤é åãããã¯ãã®ä¸¡æ¹ã§åå¾
- mysqli_result::fetch_array
- çµæã®è¡ãé£æ³é åã»æ°å¤é åãããã¯ãã®ä¸¡æ¹ã§åå¾
- mysqli_result::fetch_assoc
- çµæã®è¡ãé£æ³é åã§åå¾ãã
- mysqli_result::fetch_row
- çµæã®è¡ãæ°å¤é åã§åå¾
- mysqli_result::fetch_count
- çµæã®ãã£ã¼ã«ãæ°ãåå¾
ããªãã¢ãã¹ãã¼ãã¡ã³ãã«ã¤ãã¦
ããªãã¢ãã¹ãã¼ãã¡ã³ãã¯ãSQLæãã³ã³ãã¤ã«ãããã³ãã¬ã¼ãã®ãããªãã®ã§ãã
æ¦å¿µã®ã¤ã¡ã¼ã¸ã¨ãã¦ã¯ãã¾ãæåã«SQLæã®ãã©ã¡ã¼ã¿ããªãé¨åï¼insertæã¨ãselectæã¨ãï¼ãç¨æãããã®å¾ã«å®éã®å¤ã¨ãªããã©ã¡ã¼ã¿ãç¨æãã¦å®è¡ããã¨ããæãã§ãã
ããã使ãå©ç¹ã¨ãã¦ã¯ããã©ã¡ã¼ã¿ãå¤ããã ãã§ä½åãåãSQLæï¼ã¯ã¨ãªï¼ãçºè¡ããªãã¦è¯ããã¨ãããã¦ãªãã¨ãã£ã¦ãSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ãé²ããã¨ãããã¨ãããã¾ãã
ããªãã¢ãã¹ãã¼ãã¡ã³ãã§ã¯ãSQLæã¨ãã©ã¡ã¼ã¿ï¼ãã¼ã¿ï¼ãå¥ã
ã«DBã«æ¸¡ãã®ã§ãSQLã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯èµ·ããªãã¨ãããã¨ã§ãã
ãã®ãããç¹ã«çç±ããªãéãã¯ããªãã¢ãã¹ãã¼ãã¡ã³ãã§ã®è¨è¿°ãæ¨å¥¨ããã¦ãã¾ãã
mysqliã§ã¯ãããªãã¢ãã¹ãã¼ãã¡ã³ãã表ãããã«mysqli_stmtã¯ã©ã¹ã使ç¨ãã¾ãã
ãã®åºæ¬çãªè¨è¿°æ¹æ³ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
<?php $mysqli = new mysqli('host', 'username', 'password', 'database'); /* æ¥ç¶ç¶æ³ããã§ãã¯ãã¾ã */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // ããã§SQLæãä½æãã¾ãï¼ãã©ã¡ã¼ã¿ã¯ããã¾ããï¼ $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); // ããã§ãã©ã¡ã¼ã¿ã«å®éã®å¤ã¨ãªãå¤æ°ãå ¥ãã¾ãã // sssdã®ã¨ããã¯ããããããã©ã¡ã¼ã¿ã®åï¼string, string, string, doubleï¼ãæå®ãã¦ãã¾ãã $stmt->bind_param('sssd', $code, $language, $official, $percent); // ããã§å¤æ°ã«å¤ãä»£å ¥ãã¦ãã¾ãã $code = 'DEU'; $language = 'Bavarian'; $official = "F"; $percent = 11.2; /* ããªãã¢ãã¹ãã¼ãã¡ã³ããå®è¡ãã¾ã */ $stmt->execute(); // $stmt->affected_rowsã§ã¯ã¨ãªçµæãåå¾ãã¦ãã¾ããããã¯Insertæãªã©ã§å¤æ´ãããè¡ã®æ°ãè¿ãã¾ãã printf("%d Row inserted.\n", $stmt->affected_rows); /* ã¹ãã¼ãã¡ã³ãã¨æ¥ç¶ãéãã¾ã */ $stmt->close(); /* æ¥ç¶ãéãã¾ã */ $mysqli->close(); ?>
ãããªæãã§ãã
ããã§ã¯$mysqli->prepareã®ã¨ããã§SQLæãä½æãããã©ã¡ã¼ã¿ã®ã¨ãããï¼ã«ãã¦ãã¾ãï¼ãã¬ã¼ã¹ãã«ãï¼*1ã
ãã®å¾ã$stmt->bind_paramã§ï¼ã®ã¨ããã«å¤æ°ããã¤ã³ãï¼çµã³ã¤ãã¦ï¼ãã¾ãã
ãããããªãã¢ãã¹ãã¼ãã¡ã³ãã®ããæ¹ã§ãã
ã¡ãªã¿ã«ã$mysql->prepareã¯æåã®ætrueã失æã®æfalseãè¿ãã®ã§ãé常ã¯ifæã§æåã®å¤æããã¾ãã
ã¾ããå¤æ°ã®å¤ãå¤ãã¦ãããä¸åº¦$stmt->execute()ãããã°ããã©ã¡ã¼ã¿ã®å¤ãå¤æ´ããã¯ã¨ãªæãçºè¡ã§ãã¾ãã
$stmt->bind_paramã®ã¨ããã§ã¯ãsã¨ãã§ãã©ã¡ã¼ã¿ã®åãæå®ãã¦ãã¾ãããä»ã«ã以ä¸ã®æåã§æå®ã§ãã¾ãã
æå | 説æ |
---|---|
i | Integerãæå® |
d | doubleãæå® |
s | stringãæå® |
b | blobãæå®ï¼ãã¾ã使ããªããï¼ |
ã¾ãã$stmt->affected_rowsã®ã¨ããã§ã¯å¤æ´ãããè¡ã®æ°ãè¿ãã¦ãã¾ãããã¯ã¨ãªçµæåå¾ã®éã«ãã使ããããªé¢æ°ã以ä¸ã«ã¾ã¨ãã¦ããã¾ãã
- mysqli_stmt::affected_rows
- INSERT, UPDATE, DELETEã¯ã¨ãªã«ãã£ã¦å¤æ´ãããè¡ã®æ°ãè¿ãï¼SELECTã®å ´åã¯num_rowsã使ãï¼
- mysqli_stmt::store_result
- SELECTæçã®çµæãè¿ãã¯ã¨ãªã®å ´åã«å®è¡ããå¿ è¦ãããé¢æ°ãï¼å¾è¿°ï¼
- mysqli_stmt::fetch
- SELECTæçã®çµæã®å¤ãåå¾ï¼å¾è¿°ï¼
- mysqli_stmt::num_rows
- SELECTã¯ã¨ãªã®çµæã®è¡æ°ãè¿ã(INSERTçã®å ´åã¯affected_rowsã使ãï¼
ããªãã¢ãã¹ãã¼ãã¡ã³ãã§å¤ãåå¾ãã
ããã§ã¯å®éã«ããªãã¢ãã¹ãã¼ãã¡ã³ãã§SELECTæçã®ã¯ã¨ãªããå¾ãããå¤ãåå¾ãã¦ã¿ããã¨æãã¾ãã
ã¾ããä»åã¯ãã¬ã¼ã¹ãã«ããï¼ã§ã¯ãªãååã«ãããã¨æãã¾ãã
<?php $mysqli = new mysqli("host", "username", "password", "database"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // ã¯ã¨ãªåãçºè¡ãã¾ãã:nameãªã©ããã¬ã¼ã¹ãã«ãã«ãªãã¾ãã $query = "select code, name from table where name=:name && value=:value"; /* ã¹ãã¼ãã¡ã³ããæºåãã¾ã */ if ($stmt = $mysqli->prepare($query)) { $name = "hoge"; $value = "huga"; // å¤æ°ããã¤ã³ããã¾ãã $stmt->bind_param(:name, $name); $stmt->bind_param(:value, $value); // å®è¡ãã¾ã $stmt->execute(); // çµæããããã¡ã«ä¿å $stmt->store_result(); // å¤æ°ãããªãã¢ãã¹ãã¼ãã¡ã³ãã«ãã¤ã³ããã¾ã // ããã§ã¯ãã¤ã³ãããã ãã§ãããå®éã«åå¾ããã®ã¯fetch() $stmt->bind_result($col1, $col2); /* å¤ãåå¾ãã¾ã */ while ($stmt->fetch()) { printf("%s %s\n", $col1, $col2); } /* ã¹ãã¼ãã¡ã³ããéãã¾ã */ $stmt->close(); } /* æ¥ç¶ãéãã¾ã */ $mysqli->close(); ?>
注æããã¨ããã¨ãã¦ã¯ä»¥ä¸ã®ç¹ã§ãã
- ååä»ããã¬ã¼ã¹ãã«ãã«ããå ´åã¯ãæåã«ã³ãã³ãä»ããã
- â»mysqliã§ã¯ä½¿ãã¾ããã§ããã»ã»ã»è©³ããã¯è¿½è¨ã§ã
- selectæãå®è¡ããå ´åã$stmt->store_result()ãå¿ ãå®è¡ãã
- $stmt->fetch()ãããåã«$stmt->bind_result()ã§å¤æ°ãããªãã¢ãã¹ãã¼ãã¡ã³ãã«ãã¤ã³ããã
ã¾ããbind_result()ã§å¤æ°ããã¤ã³ãããã«fetch_array()ãfetch_assoc, fetch_all()ã§é£æ³é
åã§åå¾ãããå ´åã¯ã以ä¸ã®ããã«ãã¾ããï¼ã¯ã¨ãªã«å¤æ°ããã¤ã³ãããã¨ããã¾ã§ã¯çç¥ãã¾ãï¼
<?php $stmt->execute(); // ã¯ã¨ãªã®å®è¡ $result = $stmt->get_result(); // ããã§çµæãåå¾ã$resultã¯$mysqli_resultã¯ã©ã¹ã¨ãã¦æ±ãã while ($row = $result->fetch_array(){ echo $row; } ?>
2013/10/06追è¨
è²ã
ã¨èª¿ã¹ãã¨ãããã©ãããmysqliã§ã¯ååä»ããã¬ã¼ã¹ãã«ãã¯ä½¿ããªãããã§ãã
PDOã§ã¯ä½¿ããã®ã§ããã»ã»ã»
ã¨ãããã¨ã§ãmysqliã®ã¨ãã¯çå符ã§ãã£ã¦ãããããªãããã§ãã
ããã¨ãmysqli_stmt::get_result()ãå®è¡ããæãé¢æ°ããªãã¨ããã¨ã©ã¼ãåºããã¨ãããã¾ãã
ããã¯å¿
è¦ãªmysqlãã©ã¤ããã¤ã³ã¹ãã¼ã«ããã¦ããªãããã«åºããã®ãããã§ãã
ãããã®ã¨ã©ã¼ãåºãå ´åã¯ããã©ã¤ããã¤ã³ã¹ãã¼ã«ãããget_result()ã使ããã«ããã®ãç¡é£ãªããã§ãã
ãã ãSelectã§æã£ã¦ããã«ã©ã ãå¤ãå ´åã¯ããã¡ãã¡å¤æ°ããã¤ã³ãããã®ã¯ããã©ãããã§ããã
こちらのブログã§ã¯mysqli_stmtã§fetch_allãå®ç¾ãã¦ããèªä½é¢æ°ãç´¹ä»ãã¦ãã¾ãã
æ¯éè¦ã¦ã¿ã¦ä¸ããã
以ä¸ã§ãã
ããªãã¢ãã¹ãã¼ãã¡ã³ãã¯ãå¤æ°ããã¤ã³ããããã¨ä»¥å¤ã¯ãã¾ãå¤ããã¾ããã
ã§ãã®ã§ã»ãã¥ãªãã£çã«ãç©æ¥µçã«ããªãã¢ãã¹ãã¼ãã¡ã³ãã使ã£ã¦ããã¾ãããï¼
ããã§ã¯ã
åèã«ããã¦ããã ãããµã¤ã
PHP: Mysqli - Manual
PHPのmysqli_stmtでfetchAllっぽいものを実現する方法 - akiyan.com
PHPでデータベースに接続するときのまとめ - Qiita [キータ]
*1:ï¼ã®ããã«å¾ããå®éã®å¤ãæ¿å ¥ããããã«ä»®ã«ç¢ºä¿ãããã®ãããã¬ã¼ã¹ãã«ããã¨ããã¾ãã