Simple PHP Database Connections
I am making a simple page to test database connection. When I tried to access it from my browser, it says:
server error
The website encountered an error retrieving
http://localhost:8888/blah/blah/test.php
. It may be out of service or misconfigured.Here are some tips:
Reload this page later. HTTP Error 500 (Internal Server Error): An unexpected condition occurred while the server was trying to fulfill the request.
All I do is connect to the database and display tables. Here's what I have before 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);
?>
My error in WAMP Apache logs:
[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 will the unexpected end of the file be?
source to share
You forgot a} at the end: 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
source to share
This is a very simple PHP database connection
<?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);
?>
Using PDO (PHP Extension) is a reliable and efficient method of connecting to MySQL. PDO also provides a lot of configuration for users and can handle exceptions as well.
The PDO connection code will look like this:
$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 PDO method, you can avoid these 500 errors and other exceptions. You can customize global error messages professionally.
source to share
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`
source to share