PHP development to implement download count statistics and create database tables
First create the database test
<?php // 創(chuàng)建連接 $conn = new mysqli("localhost", "uesename", "password"); // 檢測(cè)連接 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error);} // 創(chuàng)建數(shù)據(jù)庫(kù) $sql = "CREATE DATABASE test"; if ($conn->query($sql) === TRUE) { echo "數(shù)據(jù)庫(kù)創(chuàng)建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
and then create a downloads table to record the file name, the file name saved on the file server and the number of downloads.
The structure is as follows:
<?php $SQL = "CREATE TABLE IF NOT EXISTS `downloads` ( `id` int(6) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(50) NOT NULL, `savename` varchar(50) NOT NULL, `downloads` int(10) unsigned NOT NULL DEFAULT '1', PRIMARY KEY (`id`), UNIQUE KEY `filename` (`filename`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; " ?>
id: It is unique, of type int, and selects the primary key.
filename: file name, type is varchar, length is 50.
savename: Downloaded file name, type is varchar, length is 50.
downloads: Number of downloads, type int.
After creating the table, add a few pieces of test data
<?php $SQL = "INSERT INTO `downloads` (`id`, `filename`, `savename`, `downloads`) VALUES (1, '下載測(cè)試1.zip', '201611.zip', 1), (2, '我要下載1.jpg', '20160901.jpg', 1), (3, 'Microsoft Office Word 文檔.docx', '20130421098547547.docx', 5), (4, 'Microsoft Office Excel 工作表.xlsx', '20130421098543323.xlsx', 12);" ?>
This completes the creation of the database table.
In order to ensure the integrity of the test function, you need to create a files folder in the local directory, and place Microsoft Office Word document.docx, Microsoft Office Excel worksheet.xlsx and other files here. folder.
Otherwise, it will prompt that the file does not exist.