php
mysql -u root -p
create database students;
use students;
GRANT SELECT,UPDATE,INSERT,DELETE ON students.* TO ‘user’@’localhost’;
//MyISAM で作ったほうがトラブルは少ないそうだ
CREATE TABLE `students` ( `ID` int(11) NOT NULL auto_increment, `Name` varchar(250) NOT NULL, `PhoneNumber` varchar(250) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT;
INSERT INTO students (Name,PhoneNumber) VALUES(‘藤本孝彦’,’0781234567′);
INSERT INTO students (Name,PhoneNumber) VALUES(‘藤本かよ子’,’0781234568);
INSERT INTO students (Name,PhoneNumber) VALUES(‘藤本太郎’,’0781234569′);
INSERT INTO students (Name,PhoneNumber) VALUES(‘藤本花子’,’0781234561);
20レコードのみを表示するようにクエリーで制限できる
$sql
=
"SELECT * FROM students ORDER BY name ASC LIMIT 0, 20"
;
最初のレコードから名前順にソートされた20レコードを返します
$sql
=
"SELECT * FROM students ORDER BY name ASC LIMIT 0, 20"
;
基本的にこの節(LIMIT start、count)では “start”は開始レコードを指定し、 “count”は表示するレコードの数を指定します
Webブラウザでindex.phpを開くと、 “students”テーブルの$start_fromレコード
から$results_per_pageレコード分を示すテーブルが表示されます。
if
(isset(
$_GET
[
"page"
])) {
$page
=
$_GET
[
"page"
]; }
else
{
$page
=1; };
$start_from
= (
$page
-1) *
$results_per_page
;
$sql
=
"SELECT COUNT(ID) AS total FROM "
.
$datatable
;
$result
=
$conn
->query(
$sql
);
$row
=
$result
->fetch_assoc();
$total_pages
=
ceil
(
$row
[
"total"
] /
$results_per_page
);
$total_pages
=
ceil
(
$total_records
/
$results_per_page
);
<?php
for
(
$i
=1;
$i
<=
$total_pages
;
$i
++) {
echo
"<a href='index.php?page="
.
$i
.
"'>"
.
$i
.
"</a> "
;
};
?>
mkdir /var/www/html/students
vi /var/www/html/students/config.php
<?php
error_reporting(0);
$servername = “localhost”;
$username = “user”;
$password = “パスワード”;
$dbname = “students”;
$datatable = “students”; // MySQL table name
$results_per_page = 20; // number of results per page
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}
?>
vi /var/www/html/students/index.php
<?php
include(‘config.php’);
if (isset($_GET[“page”])) { $page = $_GET[“page”]; } else { $page=1; };
$start_from = ($page-1) * $results_per_page;
$sql = “SELECT * FROM “.$datatable.” ORDER BY ID ASC LIMIT $start_from, “.$results_per_page;
$rs_result = $conn->query($sql);
?>
<table border=”1″ cellpadding=”4″>
<tr>
<td bgcolor=”#CCCCCC”><strong>ID</strong></td>
<td bgcolor=”#CCCCCC”><strong>Name</strong></td><td bgcolor=”#CCCCCC”><strong>Phone</strong></td></tr>
<?php
while($row = $rs_result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row[“ID”]; ?></td>
<td><?php echo $row[“Name”]; ?></td>
<td><?php echo $row[“PhoneNumber”]; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = “SELECT COUNT(ID) AS total FROM “.$datatable;
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$total_pages = ceil($row[“total”] / $results_per_page); // calculate total pages with results
for ($i=1; $i<=$total_pages; $i++) { // print links for all pages
echo “<a href=’index.php?page=”.$i.”‘”;
if ($i==$page) echo ” class=’curPage'”;
echo “>”.$i.”</a> “;
};
?>
コメント