Database information file, for example db-info.php
$server = "localhost"; $user = "admin"; $pass = "demo"; $database = "my_database";
The page to which we want to connect the database, for example index.php
include("db-info.php"); $link = mysql_connect($server, $user, $pass); if(!mysql_select_db($database)) die(mysql_error());
All now we can on the index.php page make any request to the database, for example, get all the information about users
$result = mysql_query("SELECT * FROM `users`"); $r = mysql_fetch_array($result); $name = $r['name'];
Now the $ name variable will select the first username of any user from the users
table
Well, so it would all look
<?php include("db-info.php"); $link = mysql_connect($server, $user, $pass); if(!mysql_select_db($database)) die(mysql_error()); $result = mysql_query("SELECT * FROM `users`"); $r = mysql_fetch_array($result); $name = $r['name']; ?> <html> <head> //сдесь содержимое хэдэра </head> <body> <p><?php echo $name; ?></p> </body> </html>
Well, of course it is not necessary to write the whole PHP script to the top of the page and you can do it more carefully, for example, via include ("file.php");