普通网友 2012-06-23 06:41
浏览 27
已采纳

MySQL在控制台中工作,但从PHP执行时会出现500内部服务器错误

I have a small php file which just creates the tables in a database. I've spent days trying to debug it and it's left me stumped. It poops out with an ugly HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request. when I execute the queries. However, when I print out the queries and paste them into the database, it works fine.

I also noticed that "table already exists" would flash when entering the queries manually, but disappear and work quickly. Similar to this question Mysql 1050 Error "Table already exists" when in fact, it does not , but I've tried deleting and remaking the database without success. If anyone could help it'd be greatly appreciated. Here's the php code

$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;
  • 写回答

1条回答 默认 最新

  • dsnm64969 2012-06-23 07:43
    关注

    HTTP Error 500 (Internal Server Error) is error response of a HTTP request.
    Use mysql_error or whatever you are using to echo the response of mysql_query in order to get error. or check error_logs. Its not exactly returned by mysql.

    If your error_reporting is off, make it ON.

    error_reporting( E_ALL | E_STRICT ); 
    ini_set( 'display_errors', 1 )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程