# Why is not that no DB is selected

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 6, 2004, 8:49am UTC](https://forum.kirupa.com/t/why-is-not-that-no-db-is-selected/42195/2 "2004-02-06T08:49:37Z")

</div>

common.php:

```php

// Database details
$dbHost = "localhost";
$dbUser = "YOU"; //root
$dbPass = "password"; //""
$dbName = "myDb";

// Common functions

/ *********************************************************
**Function: dbconnect()**
 **Desc: Perform database server connection and**
 **database selection operations**
 ********************************************************* /

function dbConnect() {
    // Access global variables
    global $dbHost;
    global $dbUser;
    global $dbPass;
    global $dbName;
    
    // Attempt to connect to database server
    $link = @mysql_connect($dbHost, $dbUser, $dbPass);

    // If connection failed...
    if (!$link) {
        // Inform Flash of error and quit
        fail("Couldn't connect to database server");
    }

    // Attempt to select our database. If failed...
    if (!@mysql_select_db($dbName)) {
        // Inform Flash of error and quit
        fail("Couldn't find database $dbName");
    }
    
    return $link;
}
function dbQueryFirst($query) {
    // Does a query and returns first row
    $result = @mysql_query($query);

	if (!$result) {
		print $query . "<br>
";
		print mysql_error() . "<br>
";
		fail("Query failed");
	}

    $returnArray = @mysql_fetch_array($result, MYSQL_ASSOC);

    @mysql_free_result($result);

    return $returnArray;
}

/ *********************************************************
**Function: fail()**
 **Params: $errorMsg - Custom error information**
 **Desc: Report error information back to Flash**
 **movie and exit the script.**
 ********************************************************* /
function fail($errorMsg) {
    // URL-Encode error message
    $errorMsg = urlencode($errorMsg);
    
    // Output error information and exit
    print "&result=Fail&errormsg=$errorMsg";
    exit;
}

 

```

to use this: in any other php:

```php
// Load in common variables and functions
include("common.php");

// Connect to MySQL Server
$link = dbConnect();
//example
// Build query to fetch post
$post = dbQueryFirst("SELECT * FROM forumPosts WHERE postID = $postID");
// Setup our variable to hold output
$output = "&postID=$postID";

// Add post details to output
$output .= "&postMessage=" . urlencode($post['message']);
$output .= "&postNotify=" . $post['notify'];
$output .= "&postAddSig=" . $post['addsig'];

// Output all data in one go
echo $output;

// Inform Flash of success
print "&result=Okay";

// Close link to database server 
mysql_close($link);

 

```
