Why is not that no DB is selected

now, in the past few days of learning PHP, i have noticed that the way to connect to a DB (connect, select, query, etc) is usually similar to the code below… now i thought, why not make that a function, call it, and pass the variables like which DB, the query, etc…

now, i have made a code… tested it out a little but none seem to work… can i put this in a function at all?

like i just do this in the php page:

ConnectSelectDB();
$searchSt = "SELECT * FROM $PORTFOLIO_TABLE LIMIT 10";
	
		//connect to db
		if(!($link=mysql_pconnect($hostName, $userName, $password))) {
			DisplayError(sprintf("error connecting to host %s, by user %s",$hostName, $userName));
			exit();
		}
	
		//select db
		if(!mysql_select_db($databaseName, $link)){
			DisplayError(sprintf("Error in selecting %s database",$databaseName));
			DisplayError(sprintf("error: %d %s",mysql_errno($link), mysql_error($link)));
			exit();
		}
	
		//execute search for all items
		if(!($result =mysql_query($searchSt, $link))){
			DisplayError(sprintf("Error in executing %s statement", $searchSt));
			echo "<BR>rowid = $rowid<BR>";
			DisplayError(sprintf("error: %d %s", mysql_errno($link), mysql_error($link)));
			exit();
		}

some examples would be nice :slight_smile:
thank you for helping out a newbie :slight_smile: