经过检查数据库,发现是数据库forum_attachment中tableid的数值不正确造成的,修复对应关系即可,修复代码如下(请注意修改高亮的行):
<?php
/**
* @name AlonePHP
* @version 0.1
* @author Tom <tom@awaysoft.com>
* @date 2014-08-20
* @description AlonePHP是一个单文件框架,适用于制作简单的小工具,支持网页及命令模式
* @copyright Apache License, Version 2.0
*/
define('ALONE_ROOT', dirname(__FILE__));
define('IS_CLI', php_sapi_name() === 'cli');
$config = [
'db' => [
'dsn' => 'mysql:host=localhost;dbname=gtk',
'user' => 'gtk',
'pass' => 'gtk',
'driver_options' => [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"]
],
];
$attachDBs = [];
function findTable($aid) {
global $attachDBs;
for ($i = 0; $i <=9; ++$i) {
$table = $attachDBs[$i];
foreach ($table as $row) {
if ($row['aid'] === $aid) {
return $i;
}
}
}
return -1;
}
function IndexController() {
$db = new Db();
global $attachDBs;
$table_pre = 'gtk_forum_attachment';
for ($i = 0; $i <= 9; ++$i) {
$sql = "select * from {$table_pre}_{$i}";
$rs = $db->query($sql);
$result = $rs->fetchAll(PDO::FETCH_ASSOC);
array_push($attachDBs, $result);
}
$sql = "select * from {$table_pre}";
$rs = $db->query($sql);
$result = $rs->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row) {
$aid = $row['aid'];
$tid = findTable($aid);
if ($tid !== -1) {
$sql = "update {$table_pre} set tableid={$tid} where aid={$aid}";
echo $aid . ':' . findTable($aid) . "\n";
$db->exec($sql);
}
}
}
/**
* 默认控制器
* Default Controller
*/
if (!function_exists('IndexController')) {
function IndexController() {
template('Index');
}
}
/**
* 默认控制器模板
* Default Controller Template
*/
if (!function_exists('IndexTemplate')) {
function IndexTemplate($args) {
echo IS_CLI ? "欢迎使用AlonePHP框架\n" : '<meta charset="utf-8">欢迎使用AlonePHP框架';
}
}
/**
* 初始化函数,可以做权限验证,数据初始化等等
* Initiazation Function, you can use it to check author or others
*/
if (!function_exists('init')) {
function init() {
}
}
/**
* 框架运行函数
* Framework run function
*/
function run() {
/* 初始化 */
init();
/* 获取控制器 */
if (IS_CLI) {
/* 获取命令模式控制器 */
$controller = param_get(1);
} else {
/* 获取网页模式 */
$controller = get('c');
}
if (!$controller) {
$controller = 'Index';
}
/* 移交控制权到相应的控制器 */
controller($controller);
}
/**
* @description 模板输出接口函数(Template function)
* @param string $name 模板名称/Template Name
* @param mixed $args 传递到模板的参数,建议用关联数组/Template Args, better using relate array.
*/
function template($name, $args = '') {
$templateName = $name . 'Template';
if (function_exists($templateName)) {
$templateName($args);
} else {
template('Error', "模板函数{$templateName}未找到!");
}
}
/**
* @description 控制器接口函数
* @param string $name 控制器名称/Controller Name
*/
function controller($name) {
$controllerName = $name . 'Controller';
if (function_exists($controllerName)) {
$controllerName();
} else {
template('Error', "控制器函数{$controllerName}未找到!");
}
}
/**
* @description 程序打开参数个数
* @return integer 返回程序的参数个数 The Number of Params
*/
function param_count() {
if (IS_CLI) {
global $argc;
return $argc - 1;
} else {
return 0;
}
}
/**
* @description 获取程序参数
* @param integer $index, 参数的位置(The position of params)
* @return string|boolean 返回第index个参数的值,The value of Params[$index];
*/
function param_get($index) {
if (IS_CLI) {
global $argc, $argv;
if ($index >= $argc) {
return false;
} else {
return $argv[$index];
}
} else {
return false;
}
}
/** GET方法
* @param string $name GET参数/The key of GET
* @param string $filter 过滤函数/Filter function
* @param string $default 默认值/Default Value
* @return string
*/
function get($name, $filter = 'htmlspecialchars', $default = '') {
if (!isset($_GET[$name])) {
$result = $default;
} else {
$result = $_GET[$name];
}
return $filter($result);
}
/** POST方法
* @param string $name POST参数/The key of POST
* @param string $filter 过滤函数/Filter function
* @param string $default 默认值/Default Value
* @return string
*/
function post($name, $filter = 'htmlspecialchars', $default = '') {
if (!isset($_POST[$name])) {
$result = $default;
} else {
$result = $_POST[$name];
}
return $filter($result);
}
/* 默认输出错误信息函数
Default Error Template
*/
function ErrorTemplate($args) {
echo $args;
}
/* 数据库类,继承自PDO */
class Db extends PDO{
private $config = [
'dsn' => '',
'user' => '',
'pass' => '',
'driver_options' => []
];
public function __construct($configNew = null) {
if (!$configNew) {
global $config;
$configNew = $config['db'];
}
$this->config = array_merge($this->config, $configNew);
try {
parent::__construct($this->config['dsn'], $this->config['user'], $this->config['pass'], $this->config['driver_options']);
} catch (Exception $e) {
trigger_error('数据库连接失败:' . $e->getMessage(), E_USER_ERROR);
}
}
}
/* 运行框架
Run Framework
*/
run();