PEARのMDB2を使ってSQLインジェクション対策(8月18日)
TetsuyaSenaha/MDB2_sample
GitHubに載っけたのを、下に表示。
database.php
<?php require_once('MDB2.php'); $db_connect = 'mysql://username:password@localhost/database?charset=utf8'; $mdb2 = MDB2::connect($db_connect); if(PEAR::isError($mdb2)){ die('接続エラー'); } ?>
index.php
<?php require_once('database.php'); //フェッチモードを設定 $mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC); $id = 1; $password = 'password'; //SQLインジェクション対策 $sth = $mdb2->prepare( 'select * from table_name where id=? AND password=?', array('integer','text'), MDB2_PREPARE_RESULT ); $result = $sth->execute(array($id,$password)); while ($row = $result->fetchRow()) { echo $row['comment']; } ?>
まあ、こんな感じです。
さて、もっとやろう。