【问题标题】:MySQL works in console but gives 500 internal server error when executed from PHPMySQL 在控制台中工作,但从 PHP 执行时出现 500 内部服务器错误
【发布时间】:2026-02-08 13:55:01
【问题描述】:

我有一个小的 php 文件,它只是在数据库中创建表。我花了几天时间尝试调试它,这让我很困惑。当我执行查询时,它会弹出一个丑陋的HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.但是,当我打印出查询并将它们粘贴到数据库中时,它工作正常。

我还注意到手动输入查询时“表已存在”会闪烁,但会消失并快速工作。类似于这个问题 Mysql 1050 Error "Table already exists" when in fact, it does not ,但我尝试删除和重建数据库但没有成功。如果有人可以提供帮助,将不胜感激。这是php代码

$users = 'CREATE TABLE users (
        id int UNSIGNED NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(id),
        username varchar(30) NOT NULL, UNIQUE(username),
        password varchar(30) NOT NULL,
        email varchar(50) NOT NULL, UNIQUE(email),
        confirmation_code int NOT NULL,
        confirmation_tries int NOT NULL
    ) ENGINE=InnoDB';

$players = 'CREATE TABLE players (
        id int UNSIGNED NOT NULL,
        FOREIGN KEY(id) REFERENCES users(id),
        cost int UNSIGNED NOT NULL,
        strategy TINYINT UNSIGNED NOT NULL,
        age int UNSIGNED NOT NULL,
        %s
    ) ENGINE=InnoDB';
$records = array();
for ($i = 0; $i < NUM_RECORDS; $i++) {
    $records[$i] = sprintf('record_%d int UNSIGNED NOT NULL', $i);
}
$players = sprintf($players, implode(', ', $records));

$kingdoms = 'CREATE TABLE kingdoms (
        id int UNSIGNED NOT NULL,
        FOREIGN KEY(id) REFERENCES users(id),
        location int UNSIGNED NOT NULL,
        building text NOT NULL,
        level int UNSIGNED NOT NULL,
        troops int UNSIGNED NOT NULL
    ) ENGINE=InnoDB';

$inboxes = 'CREATE TABLE inboxes (
        recipientID int UNSIGNED NOT NULL,
        FOREIGN KEY(recipientID) REFERENCES users(id),
        messageID int UNSIGNED NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(messageID),
        message text NOT NULL,
        date DATETIME NOT NULL
    ) ENGINE=InnoDB';

$lastActions = 'CREATE TABLE last_actions (
        id int UNSIGNED NOT NULL,
        FOREIGN KEY(id) REFERENCES users(id),
        history text
    ) ENGINE=InnoDB';

$news = 'CREATE TABLE news (
        id int UNSIGNED NOT NULL,
        FOREIGN KEY(id) REFERENCES users(id),
        entries text
    ) ENGINE=InnoDB';

/*
$dbh->exec($users);
$code = $dbh->errorCode();
if ($code !== '00000') {
    var_dump($code);
    echo '111111';
    exit;
}
$dbh->exec($players);
$code = $dbh->errorCode();
if ($code !== '00000') {
    var_dump($code);
    echo '22222';
    exit;
}
$dbh->exec($kingdoms);
$code = $dbh->errorCode();
if ($code !== '00000') {
    var_dump($code);
    echo '33333';
    exit;
}
$dbh->exec($inboxes);
$code = $dbh->errorCode();
if ($code !== '00000') {
    var_dump($code);
    echo '44444';
    exit;
}
$dbh->exec($lastActions);
$code = $dbh->errorCode();
if ($code !== '00000') {
    var_dump($code);
    echo '555555';
    exit;
}
$dbh->exec($news);//
$code = $dbh->errorCode();
if ($code !== '00000') {
    var_dump($code);
    echo '666666';
    exit;
}//*/
echo $users . ";" . $players . ";" . $kingdoms . ';' . $inboxes . ';' . $lastActions .';' . $news;

【问题讨论】:

  • 添加错误报告(E_ALL | E_STRICT); ini_set('display_errors', 1);到你的 PHP 文件的开头,看看发生了什么。
  • 这不是你的 SQL 本身给出的错误:检查你的错误日志,它可能是你的 PHP 代码、你的 htaccess 等中的东西。你应该 a) 检查你的错误日志是否有错误, b) 向我们展示一段最小的具有行为的代码,而不是这个大的东西(大部分都被注释掉了?)
  • @MaxSem 谢谢 - 又花了几个小时,但最终解决了这个问题。我的主人发生了一些时髦的事情。如果您想发布作为答案,我会选择它作为正确的

标签: php mysql innodb


【解决方案1】:

HTTP 错误 500(内部服务器错误)是 HTTP 请求的错误响应。
使用 mysql_error 或您正在使用的任何内容来回显 mysql_query 的响应以获取错误。或检查 error_logs。 它不完全由 mysql 返回。

如果您的 error_reporting 已关闭,请将其设为 ON。

error_reporting( E_ALL | E_STRICT ); 
ini_set( 'display_errors', 1 )

【讨论】:

  • error_reporting... 部分有所帮助,但这是来自早期用户。另外,我使用的不是过时的mysql_... 函数,而是PDO 连接和语句。仍然会选择它,但另一个人是第一个。对不起!
最近更新 更多