我正在制作一个简单的页面来测试数据库连接。当我尝试从浏览器访问它时,它说:
服务器错误
网站在检索
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);
?>
我在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
文件意外结束会是什么?
答案 0 :(得分: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
答案 1 :(得分:3)
这是非常简单的PHP数据库连接
<?php
$dbhost = "dbhostname";
$dbusername = "dbusername";
$dbpassword = "dbpassword";
$dbname = "dbname";
$connection = mysql_connect($dbhost, $dbusername, $dbpassword) or die('Could not connect');
$db = mysql_select_db($dbname);
?>
答案 2 :(得分:2)
使用PDO(PHP扩展)是连接MySQL的可靠而有效的方法。 PDO还为用户提供了更多配置,它也可以处理异常。
PDO连接代码如下:
$hostname='localhost';$username='root';$password='';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=stickercollections",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line echo 'Connected to Database<br/>';
} catch(PDOException $e) {
echo $e->getMessage();
}
通过使用PDO方法,您可以避免此500错误和其他异常。您可以以专业的方式配置全局错误消息。
答案 3 :(得分:0)
此外,$database
是未定义的变量。在您的上下文中应该$dbname
代替$database
。
答案 4 :(得分:-1)
var $hostname;
var $username;
var $password;
var $database;
public function dbconnect($hostname=DB_HOST, $username=DB_USER, $password=DB_PASS, $database=DB_NAME)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->connect();
$this->select_db();
}
public function connect()
{
$this->handler = mysql_pconnect($this->hostname, $this->username, $this->password) or $this->throw_error(mysql_error(), __LINE__);
}
public function select_db()
{
mysql_select_db($this->database, $this->handler) or $this->throw_error(mysql_error(), __LINE__);
}
public function DoSelect($query)
{
$result = mysql_query($query);
$grandvalue="";
$RecordCounter=0;
if(!mysql_error() && mysql_num_rows($result))
{
if(mysql_num_rows($result)>0)
{
// Make sure we get some rows back before we try to fetch them
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{
$grandvalue[$RecordCounter]=$row;
$RecordCounter++;
}
return $grandvalue;
}
else
{
return null;
}
}
}
public function DoInsert($query)
{
//echo $query;
$this->dbconnect();
$val=mysql_query($query);
// echo $val;
}
public function DoUpdate($query)
{
$this->dbconnect();
$val=mysql_query($query);
return $val;
}
public function DoDelete($query)
{
$this->dbconnect();
mysql_query($query);
}
}
$connection = new mysql_db();
$connection->dbconnect();
?>`enter code here`