PHP Basics - Connecting to MySQL
Josh-
Royal Oak, MI
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..
What does each variable define, you ask?
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
-- 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
0
Comments
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.