im wondering what is the best and safest way to make a connection to a database to include in all the php files where i have to get data from the database. right now i always make a connection every single time lets say i use a while to get some data from a certain table from database that looks like the code below. Can you guys tell me what is the best way to make a connection and use it for the while?
How i make a connection for the while now every single time
$id=$_SESSION['id'];
$connect = mysql_connect("localhost", "username", "password");
$select_data = mysql_select_db('databasename', $connect);
$select_data = mysql_query("SELECT * FROM members WHERE `id`='$id'") or die(mysql_error());
while($fetch=mysql_fetch_assoc($select_data)) {
// Show profile pic.
$oImgBox = $dom->getElementById('adminProfilePicture');
$oImg = $dom->createElement('image');
$oImg->setAttribute('src',$fetch["profilepic"]);
$oImgBox->appendChild($oImg);