# Why is not that no DB is selected

**URL:** <https://forum.kirupa.com/t/why-is-not-that-no-db-is-selected/101132>\
**Category:** Uncategorized\
**Created:** [February 6, 2004, 8:30am UTC](https://forum.kirupa.com/t/why-is-not-that-no-db-is-selected/101132 "2004-02-06T08:30:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sinus\_](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sinus_/32/417_2.png) [@sinus\_](https://forum.kirupa.com/u/sinus_)\
**Post date:** [February 6, 2004, 8:30am UTC](https://forum.kirupa.com/t/why-is-not-that-no-db-is-selected/101132/1 "2004-02-06T08:30:13Z")

</div>

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:

```php
ConnectSelectDB();

```

```php
$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 🙂  
thank you for helping out a newbie 🙂
