• Quick and easy access to MySQL database

    I normally create a functions.php file for all my websites, which is then called into every page.  One of the first things that this functions file does, is connect to the appropriate database for the website.

    Now and then, however, you need to manually call a database connection, before the functions file is loaded, for example, if you’re testing a condition and redirecting a user using the header() command in PHP.

    The quickest way I’ve found to create your PHP/MySQL interaction is as follows:

    1
    2
    3
    // CONNECT TO DATABASE
    mysql_connect("localhost", "dbname_username", "password") or die(mysql_error());
    mysql_select_db("dbname_tablename") or die(mysql_error());

    Obviously, you’d swap out the username, password and table name in the above, but it’s a handy snippet of code to have near to hand.

    Share and Enjoy:
    • Digg
    • Sphinn
    • del.icio.us
    • Facebook
    • Mixx
    • Google Bookmarks

    3 responses to “Quick and easy access to MySQL database”


    • Thomas Read

      Thanks for writing about this. There’s a heap of good information on the www. You got a lot of good info here on your web site. I’m impressed – I try to keep a couple blogs pretty on-going, but it’s a struggle sometimes.


    • how to fix blue screen

      wow, awesome post, I was wondering


    • theoneandonlytruzer

      I have needed this information for so long. Where have you been!!! Thanks


    1 Trackbacks / Pingbacks

     Leave a reply