mysql -u root -p
create database book;
use book;
CREATE TABLE book (
id int(11) NOT NULL auto_increment,
title varchar(300) NOT NULL,
author varchar(100) NOT NULL,
price float(255, 2) NOT NULL,
PRIMARY KEY (id)
) ;
INSERT INTO book (title,author,price) VALUES (‘HTML and CSS : Design and Build Websites Jon Duckett’,’Jon Duckett’,15.94);
INSERT INTO book (title,author,price) VALUES (‘Web Design All-in-One For Dummies’,’Sue Jenkins’,29.15);
INSERT INTO book (title,author,price) VALUES (‘PHP and MySQL 24-Hour Trainer’,’Andrea Tarr’,27.00);
INSERT INTO book (title,author,price) VALUES (‘PHP Solutions: Dynamic Web Design Made Easy’,’David Powers’,23.65);
INSERT INTO book (title,author,price) VALUES (‘Beginning CSS3′,’David Powers’,25.70);
GRANT SELECT,UPDATE,INSERT,DELETE ON book.* TO ‘user’@’localhost’;
mkdir /var/www/html/book
vi /var/www/html/book/database-search-form.html
<html>
<head>
<title> How To Create A Database Search With MySQL & PHP Script </title>
</head>
<body>
<form action=”php-database-search.php” method=”post”>
検索: <input type=”text” name=”search” placeholder=” 検索 … “/>
<input type=”submit” value=”送信” />
</form>
</body>
</html>
vi /var/www/html/book/php-database-search.php
<form action=”php-database-search.php” method=”post”>
検索: <input type=”text” name=”search” placeholder=”検索…”/>
<input type=”submit” value=”送信” />
</form>
<?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) {
echo “検索結果 :<br/>”;
echo “<table style=\”font-family:arial;color:#333333;\”>”;
echo “<tr><td style=\”border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\”>題名</td><td style=\”border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\”>著者</td><td style=\”border-style:solid;border-width:1px;border-color:#98bf21;background:#98bf21;\”>価格</td></tr>”;
while ($results = $query->fetch()) {
echo “<tr><td style=\”border-style:solid;border-width:1px;border-color:#98bf21;\”>”;
echo $results[‘title’];
echo “</td><td style=\”border-style:solid;border-width:1px;border-color:#98bf21;\”>”;
echo $results[‘author’];
echo “</td><td style=\”border-style:solid;border-width:1px;border-color:#98bf21;\”>”;
echo “$”.$results[‘price’];
echo “</td></tr>”;
}
echo “</table>”;
} else {
echo ‘Nothing found’;
}
?>
パスワードを書き換える
コメント