Trouble connecting PHP to MySQL

I am having trouble getting my PHP to connect to my SQL Server and database. When I run the following code, the page does not load. It comes up with the following message in IE. I have the page in my htdocs folder for Apatche server. Other pages work fine, just not this one. (the only difference in the following code to my code is that I have changed the password - I actually have a database called my_database).

This error (HTTP 500 Internal Server Error) means that the website you are visiting had a server problem which prevented the webpage from displaying.

For more information about HTTP errors, see Help


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Connect Test</title>
</head>

<body>
<h1>Connect Test</h1>

<?php
$link = mysqli_connect('localhost', 'root', 'my_password');
if (!$link)
{
	$output = 'Unable to connect to the database server.';
	include 'output.html.php';
	exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'output.html.php';
	exit();
}

if (!mysqli_select_db($link, 'my_database'))
{
	$output = 'Unable to locate the my_database database.'
	include 'output.html.php';
	exit();
}

$output = 'Database connection established.';
include 'output.html.php';
?-->

</body>
</html>

This is my output page code:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Connect Test</title>
</head>

<body>
<h1>Output</h1>

<p><?php echo $output ?>;
?>

</body>
</html>

Your help you be appreciated. Thank you in advance.

you didnot closed php
?> missing

I am a beginner so if its wrong I’m sorry…

Hi John,

Your first script is including the file output.html.php to display any results / errors, so you don’t need that HTML surrounding your PHP:


<?php 

$link = mysqli_connect('localhost', 'root', 'my_password'); 
if (!$link) 
{ 
    $output = 'Unable to connect to the database server.'; 
    include 'output.html.php'; 
    exit(); 
} 

if (!mysqli_set_charset($link, 'utf8')) 
{ 
    $output = 'Unable to set database connection encoding.'; 
    include 'output.html.php'; 
    exit(); 
} 

if (!mysqli_select_db($link, 'my_database')) 
{ 
    $output = 'Unable to locate the my_database database.' 
    include 'output.html.php'; 
    exit();  
} 

$output = 'Database connection established.'; 
include 'output.html.php';

Note: with pure PHP files (i.e. they don’t contain any markup) you don’t need to include the closing PHP tag (?>) and it’s recommended that you don’t

I now have the following. however, it still does not work. It just comes up with the same HTTP 500 Internal Server error as before.


<?php 
$link = mysqli_connect('localhost', 'root', 'my_password');
if (!$link)
{
	$output = 'Unable to connect to the database server.';
	include 'output.html.php';
	exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'output.html.php';
	exit();
}

if (!mysqli_select_db($link, 'my_database'))
{
	$output = 'Unable to locate the my_database database.'
	include 'output.html.php';
	exit(); 
}

$output = 'Database connection established.';
include 'output.html.php';

Second file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Connect Test</title>
</head>

<body>
<h1>Output</h1>

<p>
<?php echo $output ?>
</p>

</body>
</html>

Online it looks as though you have error_reporting(0); and ini_set(‘display_errors’,FALSE); so you do not notice the syntax errors :slight_smile:

Try this:


error_reporting(-1);ini_set('display_errors',true);

...
...

$output = 'Unable to locate the my_database database.'

Parse error:  syntax error, unexpected 'include' (T_INCLUDE) in /opt/lampp/htdocs/index.php on line 23


Hi John. I am only a beginner. I do not know what to do with the code you quoted. I have copied it into my file. I am just trying to follow the book’s standard example of connecting to a MySQL database, which I have never done before. All I want to do is connect to the MySQL database I have installed with PHP.

Ok, no problem about being being a beginner, we have all been there and struggled.

Please take special note of the error message which shows because the error_reporting(-1); is set to maximum and because of ini_set(‘display_errors’, true); the errors will be displayed on the screen and not in your error reporting file.

Parse error: syntax error, unexpected ‘include’ FONT=monospace [/FONT]in /opt/lampp/htdocs/index.php on line 23


// missing semi-colon at end of next line:

$output = 'Unable to locate the my_database database.'