skip to content rich footer

stevenclark.com.au

subscibe to the StevenClark.com.au rss feed

SQL ConnectToDatabase Function

Carrying on from my last post about connecting to an SQL database using PHP, here are the functions I use to open and close database connections (Note: » just extends the line, you’ll need to remove that for this to work correctly).

// to open the database
function connectToDatabase ()	{
$link = mysql_connect("localhost", "database_name", »
                                  "database_password");
    if ($link && mysql_select_db("database_name"));
        return ($link);
	    return (FALSE);
}

// to close the database
function db_close($connecter)  {
    mysql_close($connecter);
}

It’s a good idea to keep these database connection functions in one place and then reuse them as you need, rather than repeating them everywhere.

Maintainability of your code is important even on smaller websites, if you only need to change a password in one place you’ll find life a lot easier. This code is very close to that found in PHP docs for mysql_connect, by the way. The connectToDatabase function returns a link identifier on success or FALSE on failure.

Further information in the mysql_connect documentation should take you an extra step as well.

So, in your PHP code where you want to run your SQL query all you need to do is something like the following snippet:


// connect to the database
include("down/in_some/folder/db_function_file.php");
$link = connectToDatabase();
    if(!link) {
        print "<p>database connection error</p>";
        mysql_close($link);
	exit();
}

Now run your query and put the values into variables, then explicitly close the link, and you’re done. Hopefully this snippet of advice might be useful.

One Response to “SQL ConnectToDatabase Function”

  1. Prevent SQL Injection Attacks in PHP : StevenClark.com.au

    [...] « SQL ConnectToDatabase Function [...]

Social Networking

Keep an eye out for me on Twitter

About the Author

Steven Clark Steven Clark - the stand up guy on this site

My name is Steven Clark (aka nortypig) and my passions are business, web development, photography and writing. I have an MBA (Specialisation) and a Bachelor of Computing from the University of Tasmania. I am working as a business management consultant.

Photography

My photography is at Steven Clark Studio and my regular photo blog presents an ongoing stream of latest images at Walk a Mile in my Shoes and I'm working on a long-term photography project called the King Island Project.

Recently Reviewed Books

Site Supporters

Hosted by Brett Drinkwater at Tashosting who is always there at the other end of my every inconvenient question and technical crisis. Brett's local community support for us over the last five years is greatly appreciated.

skip to top of page

Currently Reading

Ansel Adams: The Camera

As the first of three parts of Ansel Adams Photography Series, Ansel Adams: The Camera begins by discussing the idea of visualisation in relation to photography. Ansel Adams is a master of his craft; this series has sat on my backburner for some time. Book 2 in this series is The Negative and it's followed up by The Print. In them Ansel outlines his philosophy of photography rather than trying to lay down a set of rules. This first instalment is a technical book that explains the good old fashion film camera.