CREATE DATABASE mydatabase;
USE mydatabase;
CREATE TABLE mytable (id INT PRIMARY KEY, name VARCHAR(50));
CREATE DATABASE mydatabase_0;
CREATE DATABASE mydatabase_1;
CREATE DATABASE mydatabase_2;
...
CREATE TABLE mytable_0 (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE mytable_1 (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE mytable_2 (id INT PRIMARY KEY, name VARCHAR(50));
...
$userId = 123456;
$databaseIndex = $userId % 3;
$tableIndex = $userId % 3;
$databaseName = "mydatabase_" . $databaseIndex;
$tableName = "mytable_" . $tableIndex;
$connection = new PDO("mysql:host=localhost;dbname=$databaseName", $username, $password);
$connection->query("SELECT * FROM $tableName WHERE id = $userId");
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com