PHP Basics - Connecting to MySQL

Josh-Josh- Royal Oak, MI
edited January 2004 in Internet & Media
PHP Basics - Connecting to MySQL Database
-- written by Josh

This short guide is for very beginners, its the basics, and will show you a way to connect to your MySQL database. First of all to start, most people (and programmers) prefer to define your MySQL database information in a different file, I define mine in a config file. Ex: Config.php

Using variables, you should easily be able to define your database and its setting. Your config file should look something similar to this..
<?

$db_Database = "mydatabase";
$db_UserName = "admin";
$db_Password = "passw";
$db_Hostname = "localhost";

?>

What does each variable define, you ask?
  • $db_Database - of course, the name of the database that you are trying to connect to
  • $db_Username - the username that has control over the database that you defined in $db_Database
  • $db_Password - the password to the username you defined in the variable above
  • $db_Hostname - the place that the server is located, e.x. 127.0.0.1..

Now, you must connect to the database.
Since you have defined all of the variables for the connection, you will not have to modify anything else now.
[php]
$db = mysql_connect("$db_Database","$db_Username","$db_Password");
[/php]

The last part..selecting the database you wish to connect to.
[php]
mysql_select_db('$db_Database', $db) or die ('Cannot connect to $db_Database : ' . mysql_error());
[/php]

Test it out, see if it works.
-Josh

Comments

  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited December 2003
    It should be pointed out that you need to restrict access to the file that contains the DB connection function so that nobody can view its contents. On Unix, chmod 644 it, and if possible, place it outside the webroot.

    If you have root control over MySQL, it's a good practice not to use root to do DB functions on the web. I create users with SELECT, INSERT, UPDATE, DELETE functions and who are allowed access to a specific DB only.
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Mr. Kwitko had this to say
    It should be pointed out that you need to restrict access to the file that contains the DB connection function so that nobody can view its contents. On Unix, chmod 644 it, and if possible, place it outside the webroot.

    If you have root control over MySQL, it's a good practice not to use root to do DB functions on the web. I create users with SELECT, INSERT, UPDATE, DELETE functions and who are allowed access to a specific DB only.

    Beat me too it..Just refreshed page..Working on some coding on my site :\ Thanks for filling everyone in for me.
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Just wondering..Could anyone tell me what the html for the php VBBC command is? I'm trying to modify the tfCode on a tForum and I would like to know how to do it. -.-
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited December 2003
    ¿Qué es VBBC?
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    vBulletin Board Code..e.x. [ ] tags instead of html < > tags
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    tForum doesn't have a BBcode for PHP, I want to make a custom one for my tForum. You guys, obviously have it on this vBulletin, so I was wondering if you guys could tell me the code for it. Unless its a php file or a seperate file other then the administrative panel, or if it voids your contract when you bought it (dont want to do nottin illegal).
  • edited January 2004
    One thing I do is that I keep all of my db connect info in a single PHP file, outside of the web root. Then I just have a single include line in each of my pages, and if I have to change the db login info, it's only once. I keep it stored outside of the web root so that no one can use the file other than myself.
Sign In or Register to comment.