简单的PHP数据库连接

wac*_*001 2 php mysql database server-side web

我正在制作一个简单的页面来测试数据库连接.当我尝试从浏览器访问它时,它说:

服务器错误

检索时网站遇到错误http://localhost:8888/blah/blah/test.php.它可能已关闭以进行维护或配置不正确.

以下是一些建议:

稍后重新载入此网页.HTTP错误500(内部服务器错误):服务器尝试完成请求时遇到意外情况.

我所做的就是连接数据库并显示表格.以下是我所拥有的PHP代码:

<?php
// Get Variables
$dbname = $_GET["dbname"];
$dbusername = $_GET["dbusername"];
$dbpass = $_GET["dbpass"];
$dbhost = $_GET["dbhost"];






$connection = mysql_connect("$dbhost","$dbusername","$dbpass");
if (!$connection)
{
    die('Could not connect: ' . mysql_error());
}
else
{
    echo "Connected";

    $dbcheck = mysql_select_db("$dbname");
    if (!$dbcheck) {
        echo mysql_error();
    }else{
        echo "<p>Successfully connected to the database '" . $database . "'</p>\n";
// Check tables
        $sql = "SHOW TABLES FROM `$database`";
        $result = mysql_query($sql);
        if (mysql_num_rows($result) > 0) {
            echo "<p>Available tables:</p>\n";
            echo "<pre>\n";
            while ($row = mysql_fetch_row($result)) {
                echo "{$row[0]}\n";
            }
            echo "</pre>\n";
        } else {
            echo "<p>The database '" . $database . "' contains no tables.</p>\n";
            echo mysql_error();
        }



    }

// some code

    mysql_close($con);



    ?>
Run Code Online (Sandbox Code Playgroud)

我在WAMP Apache日志中的错误是:

[03-Feb-2013 22:47:37 UTC] PHP Parse error:  syntax error, unexpected end of file                              in /Applications/MAMP/htdocs/coursemanager/default/verify1.php on line 52
Run Code Online (Sandbox Code Playgroud)

文件意外结束会是什么?

Gre*_*ack 8

你最后忘了关闭}: else{ echo "Connected";

<?php

// Get Variables
$dbname = $_GET["dbname"];
$dbusername = $_GET["dbusername"];
$dbpass = $_GET["dbpass"];
$dbhost = $_GET["dbhost"];


$connection = mysql_connect("$dbhost", "$dbusername", "$dbpass");
if (!$connection) {
    die('Could not connect: ' . mysql_error());
} else {
    echo "Connected";

    $dbcheck = mysql_select_db("$dbname");
    if (!$dbcheck) {
        echo mysql_error();
    } else {
        echo "<p>Successfully connected to the database '" . $database . "'</p>\n";
// Check tables
        $sql = "SHOW TABLES FROM `$database`";
        $result = mysql_query($sql);
        if (mysql_num_rows($result) > 0) {
            echo "<p>Available tables:</p>\n";
            echo "<pre>\n";
            while ($row = mysql_fetch_row($result)) {
                echo "{$row[0]}\n";
            }
            echo "</pre>\n";
        } else {
            echo "<p>The database '" . $database . "' contains no tables.</p>\n";
            echo mysql_error();
        }
    }
}
// some code. no need to close mysql and no need to close php
Run Code Online (Sandbox Code Playgroud)