PHP Object-oriented PHP data Object (PDO)--Book View Management

Source: Internet
Author: User
Tags dsn

First create related database: Database name I use the books table name is the book (the following is the complete database, the img/img01.jpg is the path to display the picture)

CREATE DATABASE Books DEFAULT Charset=utf8;
Use books;

CREATE TABLE Book (
b_id INT PRIMARY KEY auto_increment,
B_name VARCHAR (+) is not NULL UNIQUE,
B_img VARCHAR () not NULL,
B_intro VARCHAR (500),
B_author VARCHAR (500),
B_house VARCHAR (500),
B_number VARCHAR (500),
B_time VARCHAR (500),
B_price VARCHAR (500),
B_stock VARCHAR (500)
) DEFAULT Charset=utf8;

INSERT into book VALUES

(NULL, ' How the Steel is tempered ', ' img/img01.jpg ', ' not included in the information ', ' Oster Lovski ', ' Beijing Hyundai Press ', ' 01020015 ', ' 1985 ', ' 058 yuan/Ben ', ' 012 '),
(NULL, ' Old Man and Sea ', ' img/img02.jpg ', ' No information ', ' Ernest Hemingway ', ' Beijing Hyundai Press ', ' 01020455 ', ' 1955 ', ' 038 yuan/Ben ', ' 054 '),
(NULL, ' Forrest Gump ', ' img/img03.jpg ', ' No related information ', ' Robert Zemeckis ', ' American Society Press ', ' 01020545 ', ' 1988 ', ' 108 Yuan/Ben ', ' 001 '),
(NULL, ' one eyebrow ', ' img/img04.jpg ', ' No related Information ', ' Lu Xun ', ' Lu Xun's training Press ', ' 01025122 ', ' 1945 ', ' 068 yuan/Ben ', ' 042 '),
(NULL, ' War and Peace ', ' img/img05.jpg ', ' not included in information ', ' Tolstoy ', ' Beijing Hyundai Press ', ' 01042015 ', ' 1955 ', ' 058 yuan/Ben ', ' 172 '),
(NULL, ' Notre Dame de Paris ', ' img/img06.jpg ', ' No related information ', ' Victor Hugo ', ' Chinese book publishing house ', ' 01095015 ', ' 1952 ', ' 055 yuan/Ben ', ' 152 '),
(NULL, ' childhood ', ' img/img07.jpg ', ' No related information ', ' Maxim Gorki ', ' Beijing Hyundai Press ', ' 01024015 ', ' 1956 ', ' 065 yuan/Ben ', ' 250 '),
(NULL, ' Wuthering Heights ', ' img/img08.jpg ', ' No related Information ', ' Emily Bronte ', ' Chinese book publishing house ', ' 01512015 ', ' 1976 ', ' 425 yuan/Ben ', ' 726 '),
(NULL, ' David Copperfield ', ' img/img09.jpg ', ' No related Information ', ' Martin Luther King ', ' National Book publishing House ', ' 01002015 ', ' 1965 ', ' 058 yuan/Ben ', ' 120 '),
(NULL, ' Red and Black ', ' img/img10.jpg ', ' No related information ', ' Stendhal ', ' National Book publishing House ', ' 01950015 ', ' 1988 ', ' 808 yuan/Ben ', ' 808 '),
(NULL, ' miserable World ', ' img/img11.jpg ', ' No related information ', ' Margaret Mitchell ', ' Beijing Hyundai Press ', ' 01220015 ', ' 1956 ', ' 508 yuan/Ben ', ' 102 '),
(NULL, ' Anna Karenina ', ' img/img12.jpg ', ' No related Information ', ' Leo Tolstoy ', ' National Book publishing House ', ' 01555015 ', ' 1936 ', ' 888 yuan/Ben ', ' 788 '),
(NULL, ' Christophe ', ' img/img01.jpg ', ' No related Information ', ' Emily Bronte ', ' Beijing Hyundai Press ', ' 01010015 ', ' 1926 ', ' 508 yuan/Ben ', ' 012 '),
(NULL, ' Gone with the Wind ', ' img/img03.jpg ', ' No information ', ' Emily Bronte ', ' Peking University Press ', ' 01052201 ', ' 1986 ', ' 445 yuan/Ben ', ' 112 '),
(NULL, ' Awake cautionary ', ' img/img05.jpg ', ' not included ', ' Feng Menglong ', ' Tsinghua Press ', ' 01024415 ', ' 1988 ', ' 718 yuan/Ben ', ' 000 '),
(NULL, ' cautionary ', ' img/img07.jpg ', ' No related information ', ' Feng Menglong ', ' China Book Press ', ' 01455515 ', ' 1956 ', ' 515 yuan/Ben ', ' 122 '),
(NULL, ' Gong expressly ', ' img/img09.jpg ', ' not included ', ' Feng Menglong ', ' Sichuan book publishing house ', ' 01020015 ', ' 1949 ', ' 528 yuan/Ben ', ' 620 '),
(NULL, ' two ticks Littleshenyang ' ', ' img/img11.jpg ', ' not included related information ', ' Ling Meng ', ' Provincial Education Network Press ', ' 10246015 ', ' 1987 ', ' 582 yuan/Ben ', ' 012 '),
(NULL, ' Zen True History ' ', ' img/img02.jpg ', ' No information ', ' Fang Ruhao ', ' Beijing Hyundai Press ', ' 15460015 ', ' 1985 ', ' 585 yuan/Ben ', ' 102 '),
(NULL, ' God Kingdoms ', ' img/img04.jpg ', ' not included ', ' Chen Zhonlin ', ' Beijing Hyundai Press ', ' 20020015 ', ' 1980 ', ' 588 yuan/Ben ', ' 312 ');

SELECT * from book;

The above is the relevant data complete information, next is the PHP code part of the use of PHP data object method:

<?php

Header ("Content-type:text/html;charset=utf-8");

Echo ' <title> book Management Information </title> ';

$dsn = ' mysql:dbname=books; host=127.0.0.1; port=3306; Charset=utf8 ';
$user = ' root ';
$password = ";

try {
$DBH = new PDO ($DSN, $user, $password);
$sql = "SELECT * from book";
$data = $dbh->query ($sql);
if ($data)
$booklist = $data->fetchall (pdo::fetch_num);

if (count ($booklist) >0) {
$bookname = $booklist [0][5];
}else{
$sql = "Select Bname from book";
$data = $dbh->query ($sql);
if ($data)
$bookname = $data->fetchall (pdo::fetch_num) [0];

}
$DBH = null;
} catch (Pdoexception $e) {
Echo ' Connection failed: '. $e->getmessage ();
}

?>
<style>
table{
border:1px solid Gray;
Border-collapse:collapse;
width:1000px;
Text-align:center;

}
Table th, table td{
border:1px solid Gray;

}
a:link {Text-decoration:none;color:blue}
a:visited{Text-decoration:none;color:blue}
</style>
<table>
<tr><th> Book serial number </th><th> book name </th><th> book cover </th><th> book Introduction </th> <th> Book author </th><th> publisher </th><th> publication number </th><th> publication time </th><th> Book prices </th><th> existing stock </th></tr>
<?php
if (Isset ($booklist) && Is_array ($booklist) && count ($booklist) >0) {
foreach ($booklist as $book) {
Echo ' <tr> ';
foreach ($book as $info) {
if (Strstr ($info, "img"))
echo "<td></td>";
Else
echo "<td><a href= ' ktlxlj.php?bname= $bookname [0] ' > $info </a></td>";

}

Echo ' </tr> ';
}
}else{
Echo ' <tr><td colspan= ' > no query to corresponding related results </td></tr> ';
}

?>
</table>

All of these are PHP object-oriented PHP data Object (PDO) code, which is done later:

PHP Object-oriented PHP data Object (PDO)--Book View Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.