サーラリマンは働き過ぎ、社畜化に注意しよう!!

book mysql php pdo bootstrap3

mysql

vi /var/www/html/book/search.html

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”utf-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<!– The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags –>
<title>Bootstrap 101 Template</title>

<!– Bootstrap –>
<link href=”css/bootstrap.min.css” rel=”stylesheet”>

<!– HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries –>
<!– WARNING: Respond.js doesn’t work if you view the page via file:// –>
<!–[if lt IE 9]>
<script src=”https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js”></script>
<script src=”https://oss.maxcdn.com/respond/1.4.2/respond.min.js”></script>
<![endif]–>
</head>
<body>

<!– jQuery (necessary for Bootstrap’s JavaScript plugins) –>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js”></script>
<!– Include all compiled plugins (below), or include individual files as needed –>
<script src=”js/bootstrap.min.js”></script>
<div class=”container”>
<h3>MySQLとPHPでBOOKデータベースを検索</h3>

<form action=”search.php” method=”post”>
本の題名:<input type=”text” name=”search” placeholder=”本の題名を入力 …”/>
<input type=”submit” value=”検索” />
</form>
</div>
</body>
</html>

 

vi /var/www/html/book/search.php

<!DOCTYPE html>
<html lang=”ja”>
<head>
<meta charset=”utf-8″>
<meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<!– The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags –>
<title>Bootstrap 101 Template</title>

<!– Bootstrap –>
<link href=”css/bootstrap.min.css” rel=”stylesheet”>

<!– HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries –>
<!– WARNING: Respond.js doesn’t work if you view the page via file:// –>
<!–[if lt IE 9]>
<script src=”https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js”></script>
<script src=”https://oss.maxcdn.com/respond/1.4.2/respond.min.js”></script>
<![endif]–>
</head>
<body>

<!– jQuery (necessary for Bootstrap’s JavaScript plugins) –>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js”></script>
<!– Include all compiled plugins (below), or include individual files as needed –>
<script src=”js/bootstrap.min.js”></script>

<div class=”container”>

<?php
//load database connection
$host = “localhost”;
$user = “user”;
$password = “パスワード”;
$database_name = “book”;
$pdo = new PDO(“mysql:host=$host;dbname=$database_name”, $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST[‘search’];
$query = $pdo->prepare(“select * from book where title LIKE ‘%$search%’ OR author LIKE ‘%$search%’ LIMIT 0 , 10”);
$query->bindValue(1, “%$search%”, PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) { ?>

<div class=”container”>
<div class=”table-responsive”>
<table class=”table”>
<thead>
<tr>
<th>ID</th>
<th>題名</th>

<th>著者</th>
<th>価格</th>
</tr>
</thead>

<?php while ($results = $query->fetch()) { ?>

<tbody>
<tr>
<td><?php echo $results[‘id’]; ?></td>
<td><?php echo $results[‘title’]; ?></td>
<td><?php echo $results[‘author’]; ?></td>
<td><?php echo $results[‘price’]; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
<?php } else {
echo ‘Nothing found’;
}
?>

</body>
</html>

コメント