问题描述
我正在制作一个简单的页面来测试数据库连接。当我尝试从浏览器访问它时,它说:
I am making a simple page to test a database connection. When I tried accessing it from my browser, it says:
该网站在检索时遇到错误 http:// localhost:8888 / blah / blah / test.php
。
The website encountered an error while retrieving http://localhost:8888/blah/blah/test.php
. It may be down for maintenance or configured incorrectly.
以下是一些建议:
稍后重新加载此网页。 HTTP错误500(内部服务器错误):服务器尝试满足请求时遇到了意外情况。
Reload this webpage later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
我正在做的就是连接到数据库并显示表格。到目前为止,这里是我的PHP代码:
All I am doing is connecting to a database and displaying the tables. Here is what I have so far as the PHP code:
<?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);
?>
我在WAMP Apache日志中的错误是:
My error in the WAMP Apache logs is:
[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
文件意外结束是什么?
推荐答案
您在结束时忘记了}: else {echo Connected;
You forgot a } on the end to close: 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
这篇关于简单的PHP数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!