Advertisement
Guest User

Untitled

a guest
Jan 20th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.90 KB | None | 0 0
  1. <?php
  2.  
  3. /* エラーを全て例外に変換する */
  4. set_error_handler(function ($errno, $errstr, $errfile, $errline) {
  5.     throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  6. });
  7.  
  8. /**
  9.  * HTML特殊文字をエスケープする関数
  10.  */
  11. function h($str) {
  12.     return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
  13. }
  14.  
  15. // クエリ変数初期化
  16. $q = '';
  17.  
  18. // フォームからクエリを受け取ったか判定
  19. if (isset($_REQUEST['q']) && is_string($_REQUEST['q'])) {
  20.     // クエリを $q にセット
  21.     $q = $_REQUEST['q'];
  22.     // 半角スペースなどのASCII制御文字や全角スペースで分割して配列化
  23.     $regex = "/[\\x0-\x20\x7f\xc2\xa0\xe3\x80\x80]++/u";
  24.     $keywords = preg_split($regex, $_REQUEST['q'], -1, PREG_SPLIT_NO_EMPTY);
  25. } else {
  26.     // 空配列
  27.     $keywords = array();
  28. }
  29.  
  30. try {
  31.  
  32.     // キーワードが1つ以上あるとき
  33.     if (!empty($keywords)) {
  34.         // DBに接続
  35.         mysql_connect('localhost', 'root', '');
  36.         // DBを選択
  37.         mysql_select_db('mydb') or trigger_error(mysql_error());
  38.         // 文字セットを指定
  39.         mysql_set_charset('utf8');
  40.         // LIKE検索用のパーツを組み立てる
  41.         foreach ($keywords as $keyword) {
  42.             $keyword = mysql_real_escape_string('%' . addcslashes($keyword, '\\_%') . '%');
  43.             $holders[] = "(`name` LIKE '{$keyword}')";
  44.         }
  45.         // SQL文生成
  46.         $sql = 'SELECT * FROM `table` WHERE (' . implode(' AND ', $holders) . ')';
  47.         // クエリ実行
  48.         $result = mysql_query($sql) or trigger_error(mysql_error());
  49.         // 件数をセット
  50.         $count = mysql_num_rows($result);
  51.         // メッセージをセット
  52.         $message = "{$count}件見つかりました";
  53.     }
  54.    
  55. } catch (ErrorException $e) {
  56.    
  57.     // 例外を捕捉してメッセージをセット
  58.     $message = $e->getMessage();
  59.    
  60. }
  61.  
  62. // ヘッダーを送出
  63. header('Content-Type: text/html; charset=utf-8');
  64.  
  65. ?>
  66. <!DOCTYPE html>
  67. <html lang="ja">
  68. <head>
  69.   <title>名前検索</title>
  70.   <style type="text/css">
  71.     table {
  72.         border: solid 3px black;
  73.     }
  74.     tr, td, th {
  75.         border: solid 1px black;
  76.         padding: 15px;
  77.     }
  78.   </style>
  79. </head>
  80. <body>
  81.   <h1>名前検索</h1>
  82.   <form action="" method="get">
  83.     <input type="text" name="q" size="30" value="<?php echo h($q) ?>">
  84.     <input type="submit" value="検索する">
  85.   </form>
  86. <?php if (isset($message)): ?>
  87.   <hr />
  88.   <h1>検索結果</h1>
  89.   <p><?=$message?></p>
  90. <?php if (!empty($result) && $count): ?>
  91.   <table>
  92.     <tr>
  93.       <th>id</th>
  94.       <th>name</th>
  95.     </tr>
  96. <?php while ($book = mysql_fetch_assoc($result)): ?>
  97.     <tr>
  98.       <td><?php echo h($book['id']) ?></td>
  99.       <td><?php echo h($book['name']) ?></td>
  100.     </tr>
  101. <?php endwhile; ?>
  102.   </table>
  103. <?php endif; ?>
  104. <?php endif; ?>
  105. </body>
  106. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement