使用数据库(DB) / 连接到数据库

<?php

$dirPath = getcwd() . "/tmp";
if (!is_dir($dirPath)) {
    mkdir($dirPath);
}

$dbPath  = "sqlite:" . $dirPath . "/data.sqlite";

// Connect to the database
$dbh  = new PDO($dbPath);

// A small piece of the "world" database
$dbh->exec("
    CREATE TABLE IF NOT EXISTS countrylanguage (
        CountryCode TEXT, Language TEXT, Percentage REAL)");
$dbh->exec("DELETE FROM countrylanguage");
$dbh->exec("
    INSERT INTO countrylanguage VALUES
        ('RUS', 'Russian', 86.6),
        ('RUS', 'Tatar', 3.2)");

$sql = "
    SELECT 
        Language, Percentage  
    FROM countrylanguage
    WHERE CountryCode = 'RUS'
    ORDER BY Percentage DESC";

foreach ($dbh->query($sql) as $row) {
    echo $row[0] . ": " . $row[1], "\n";
}
$dbh = null