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
thank you for helping out a newbie
// 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:
// 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);