警告-此页面上的某些功能需要的浏览器或操作系统比您当前使用的浏览器或操作系统更新。 请在其他浏览器打开该页面。
连接到数据库并运行SQL语句
API文档- 此页面使用带有SQL的数据库来保存和显示最近的页面浏览量和HTTP请求.
- 使用控件提交其他请求,或使用其他浏览器或开发工具尝试此页以查看不同的用户代理.
- 此页的数据库示例代码和PHP代码如下表所示.
- 您可以通过单击表中的某一行来突出显示它.
- 红
- 绿
- 蓝
- 黄
Id | URL | Method | Device Type | OS Type | OS | Browser | 用户代理 | 请求日期 |
---|---|---|---|---|---|---|---|---|
1465 | /zh-CN/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com) | 2024-11-23 10:14:24 | ||
1464 | /fr/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/) | 2024-11-22 19:58:36 | ||
1463 | /ar/examples/database-demo | GET | Bot | Android | Android 6.0.1 | Chrome | Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.85 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) | 2024-11-22 16:13:23 |
1462 | /zh-CN/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/) | 2024-11-22 16:09:56 | ||
1461 | /zh-CN/examples/database-demo | GET | Bot | Android | Android 6.0.1 | Chrome | Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.85 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) | 2024-11-22 14:57:10 |
1460 | /es/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; ImagesiftBot; +imagesift.com) | 2024-11-22 13:29:10 | ||
1459 | /zh-CN/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; MJ12bot/v1.4.8; http://mj12bot.com/) | 2024-11-22 13:27:07 | ||
1458 | /pt-BR/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; MJ12bot/v1.4.8; http://mj12bot.com/) | 2024-11-22 13:26:54 | ||
1457 | /fr/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; MJ12bot/v1.4.8; http://mj12bot.com/) | 2024-11-22 13:26:41 | ||
1456 | /es/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; MJ12bot/v1.4.8; http://mj12bot.com/) | 2024-11-22 13:26:28 | ||
1455 | /en/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; ImagesiftBot; +imagesift.com) | 2024-11-22 13:04:09 | ||
1454 | /en/examples/database-demo | GET | Bot | Android | Android 6.0.1 | Chrome | Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.85 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) | 2024-11-22 13:02:58 |
1453 | /pt-BR/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; ImagesiftBot; +imagesift.com) | 2024-11-22 12:59:43 | ||
1452 | /fr/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; ImagesiftBot; +imagesift.com) | 2024-11-22 12:49:03 | ||
1451 | /es/examples/database-demo | GET | Bot | Android | Android 6.0.1 | Chrome | Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.85 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) | 2024-11-22 12:47:48 |
1450 | /ar/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; ImagesiftBot; +imagesift.com) | 2024-11-22 12:33:27 | ||
1449 | /en/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/) | 2024-11-22 11:58:11 | ||
1448 | /zh-CN/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; ImagesiftBot; +imagesift.com) | 2024-11-22 11:19:11 | ||
1447 | /es/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/) | 2024-11-22 09:35:15 | ||
1446 | /ar/examples/database-demo | GET | Bot | Bot | Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/) | 2024-11-22 09:01:27 |
数据库代码概述
// Connect to a Database
$db = new \FastSitePHP\Data\Database($dsn, $user, $password);
// Query for Multiple Records
// Returns an Array of Records (Associative Array for each Record).
$sql = 'SELECT * FROM pages';
$records = $db->query($sql);
// Query for one record. Returns an Associative Array or [null] if not found.
// Both [query()] and [queryOne()] support optional parameters when querying.
$sql = 'SELECT * FROM pages WHERE id = ?';
$params = [1];
$record = $db->queryOne($sql, $params);
// Use [execute()] for INSERT, UPDATE, DELETE, CREATE and other Transactions.
// In addition to using [?] you can also used named parameters in the
// format of ":name". Named parameters can make the code easier to read.
$sql = 'INSERT INTO pages (title, content)';
$sql .= ' VALUES (:title, :content)';
$params = [
'title' => 'Database Demo',
'content' => '<h1>Database Demo<h1>',
];
$rows_affected = $db->execute($sql, $params);
// Many additional examples can be found on the quick reference page.
此页的PHP代码
<?php
namespace App\Controllers\Examples;
use FastSitePHP\Application;
use FastSitePHP\Data\Database;
use FastSitePHP\Lang\I18N;
use FastSitePHP\Web\Request;
class DatabaseDemo
{
/**
* Return HTML for the Web Page
*/
public function get(Application $app, $lang)
{
// Load Language File
I18N::langFile('database-demo', $lang);
// Add Code Example from text file
$file_path = $app->config['I18N_DIR'] . '/code/database-demo.{lang}.txt';
$app->locals['i18n']['db_code'] = I18N::textFile($file_path, $app->lang);
// Add a record for the request and get the 20 most recent records
$records = $this->insertAndSelectRecords($app);
// Render the View
$templates = [
'old-browser-warning.htm',
'examples/database-demo.php',
'examples/database-demo.htm',
'table-highlighter.htm',
];
return $app->render($templates, [
'nav_active_link' => 'examples',
'records' => $records,
'controller_code' => file_get_contents(__FILE__),
]);
}
/**
* JSON Service that logs requests from button clicks.
* Returns the 20 most recent records.
*/
public function routePage(Application $app, $lang, $color)
{
return ['records' => $this->insertAndSelectRecords($app)];
}
/**
* Add a record for the request and return recent records
*/
private function insertAndSelectRecords(Application $app)
{
$db = $this->connectToDb();
$this->insertRecord($app, $db);
$records = $this->getRecentRecords($db);
return $records;
}
/**
* Return a Db Connection to a SQLite Database in the Temp
* Directory. Create the file if it doesn't yet exist.
*/
private function connectToDb()
{
// Path to SQLite Db
$path = sys_get_temp_dir() . '/database-demo.sqlite';
$this->checkDb($path);
// Connect and create table the first time the db is used
$dsn = 'sqlite:' . $path;
$db = new Database($dsn);
$sql = <<<'SQL'
CREATE TABLE IF NOT EXISTS requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT,
method TEXT,
user_agent TEXT,
date_requested TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
SQL;
$db->execute($sql);
return $db;
}
/**
* Delete the SQLite Database every time it reaches over 10 megabytes.
* It is intended only as a temporary database for this demo page.
*/
private function checkDb($path) {
if (is_file($path)) {
$ten_megabytes = (1024 * 1024 * 10);
$file_size = filesize($path);
if ($file_size > $ten_megabytes) {
unlink($path);
// Add to a log file each time it's removed
$log_path = sys_get_temp_dir() . '/database-demo.txt';
$now = date(DATE_RFC2822);
$contents = "${path} deleted at ${now}\n";
file_put_contents($log_path, $contents, FILE_APPEND);
}
}
}
/**
* Insert a Record for each Request
*/
private function insertRecord(Application $app, Database $db)
{
$req = new Request();
$sql = 'INSERT INTO requests (url, method, user_agent) VALUES (?, ?, ?)';
$params = [$app->requestedPath(), $req->method(), $req->userAgent()];
$db->execute($sql, $params);
}
/**
* Return the 20 most recent records.
* The SELECT statement is defined in a [*.sql] file,
* which allows for easy editing and testing outside of PHP code.
*/
private function getRecentRecords(Database $db)
{
$sql = file_get_contents(__DIR__ . '/../../Models/DatabaseDemo.sql');
$records = $db->query($sql);
return $records;
}
}