PHPの勉強で蔵書検索システムを作りましたが、エラーが表示されます。
何がいけないのでしょうか。ソースは下記です。
lang
1search.php 2 3<?php 4$buf = ""; 5 6if ( isset($_GET["word"]) ) { 7 8 $word = $_GET["word"]; 9 $arr = explode(" ", $word); 10 11 $sql = "SELECT * FROM books WHERE "; 12 for ( $i = 0; $i < count($arr); $i++ ) { 13 $sql .= "title LIKE '%" . $arr[$i] . "%' "; 14 } 15 16 // Database 17 $dsn = 'mysql:dbname=dbname;host=localhost'; 18 $user = 'user'; 19 $password = 'password'; 20 21 try{ 22 $dbh = new PDO($dsn, $user, $password); 23 } catch ( PDOException $e ) { 24 print ( 'Error:'. $e->getMessage() ); 25 die(); 26 } 27 $stmt = $dbh->query($sql); 28 if ( $stmt->fetchColumn() > 0 ) { 29 while ( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { 30 $buf .= $row["author"] . ":" . $row["title"] . "<br />\n"; 31 } 32 } else { 33 $buf .= $word . "にマッチするデータはありませんでした。"; 34} 35$dbh = null; 36} 37?> 38<html> 39<meta http-equiv="content-type" content="text/html; charset=utf-8"> 40<body> 41<form action="./search.php" method="get"> 42<input type="text" name="word"><input type="submit" value=" 検索 "> 43</form> 44<?php print $buf; ?> 45</body> 46</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。