简单的 PHP 数据库连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14718260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple PHP Database Connections
提问by waco001
I am making a simple page to test a database connection. When I tried accessing it from my browser, it says:
我正在制作一个简单的页面来测试数据库连接。当我尝试从浏览器访问它时,它说:
Server error
The website encountered an error while retrieving
http://localhost:8888/blah/blah/test.php. It may be down for maintenance or configured incorrectly.Here are some suggestions:
Reload this webpage later. HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
服务器错误
该网站在检索
http://localhost:8888/blah/blah/test.php. 它可能因维护而停机或配置不正确。以下是一些建议:
稍后重新载入此网页。HTTP 错误 500(内部服务器错误):服务器尝试完成请求时遇到意外情况。
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 代码:
<?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);
?>
My error in the WAMP Apache logs is:
我在 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
What would a unexpected end of file be?
意外的文件结尾是什么?
回答by Green Black
You forgot a } on the end to close: else{ echo "Connected";
你最后忘记了一个 } 来关闭: 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
回答by Green Black
It is very simple PHP database connection
非常简单的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);
?>
回答by Brown Parker
Using the PDO (PHP extension) is reliable and efficient method to connect to MySQL. PDO also gives more configuration to users and it can handle exception also.
使用 PDO(PHP 扩展)是连接 MySQL 的可靠且有效的方法。PDO 还为用户提供了更多的配置,它也可以处理异常。
PDO connection code will be like this:
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();
}
By using the PDO method, you can avoid this 500 errors and other exceptions. You can configure global error messages in a professional way.
通过使用 PDO 方法,您可以避免此 500 错误和其他异常。您可以以专业的方式配置全局错误消息。
Reference : Connect php to database
参考:将php连接到数据库
回答by Kite_runner
Also, $databaseis undefined variable. It should be $dbnamein place of $databasein your context.
此外,$database是未定义的变量。它应该在您的上下文$dbname中代替$database。
回答by user3070157
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`

