
Connect to MYSQL Database
Tutorial by: Epatron
- Connect to MYSQL
- Introductions
- The PHP Code
- Explanation
- Introductions
- Selecting Database
- The PHP Code
- Explanation
- The PHP Code
- Putting them together
- The PHP Code
-Introductions:
Before start reading, please notice that I'm from finland and our language isn't english!
At this part we're going to create PHP file with connection code inside of it.
Connecting to mysql is really easy to do and learn!
We're going to use PHP function called: mysql_connect()
Statement: mysql_connect(servername,username,password);
I'm going to teach the meanings at "Explanation" part!
-The PHP Code:
Create file named connect.php, and start writing the code:
I really recommend to always write by yourself the php code if it's not so long
But if you're lazy, you can just copy paste and continue to "Explanation" part!
<?php //Variables for the connection: $m_Host = "Your HOST here"; $m_User = "Your USERNAME here"; $m_Pass = "Your PASSWORD here"; //Code for connection: $connect = mysql_connect($m_Host,$m_User,$m_Pass) or die(mysql_error()); echo '<b style="color:#298A08;">Connected to MYSQL!</b>'; ?>
-Explanation:
Let's start explaining the code!
$m_Host = "Your HOST here";I created a variable with name "m_Host". I actually shortened it from mysql_Host, but remember that variable can be any other word too!
Example: $host, $h000000000000000000st, $connect_Host!
At the "Your HOST here" you need to put your servername!
Example: If you're using
Xampp: localhost
or if you're using
000webhost: Go to CPANEL and MYSQL > Create or check your host!
NOTE: REMEMBER TO STILL INCLUDE THE QUOTES EG. "localhost"
$m_User = "Your USERNAME here";Variable with name "m_User".
"Your USERNAME here" part you need to add your username.
Example: If you're using XAMPP: root or 000webhost: CPANEL > MYSQL > Create or check your username!
$m_Pass = "Your PASSWORD here";"Your PASSWORD here" part you need to add your password!
Example: If you're using XAMPP: put your password or 000webhost: CPANEL > MYSQL > Create or check your password!
$connect = mysql_connect($m_Host,$m_User,$m_Pass) or die(mysql_error());I Created variable connect which includes the connection function! (Why? You'll get it at the select database part)
Now the mysql_connect() function uses our variables $m_Host,$m_User and $m_Pass.
or die(mysql_error()); means that if connection fails you're getting a mysql error note!
echo '<b style="color:#298A08;">Connected to MYSQL!</b>';If connection runs perfectly you'll get text which is styled with color and bolded like this:
Connected to MYSQL!
Selecting Database
-The PHP Code:
<?php //Variable for selecting db: $m_Db = "Your DB NAME here"; //Code for selecting db: mysql_select_db($m_Db,$connect) or die(mysql_error()); ?>-Explanation:
"Your DB NAME here" part put your database name, if you don't have database get in your phpmyadmin and create the database!
And for the selecting the database we're using the mysql_select_db function and function is using like in mysql_connect our variables. In this case $m_Db.
If selecting fails you will get mysql error note!
Putting them together
<?php //Variables for the connection: $m_Host = "Your HOST here"; $m_User = "Your USERNAME here"; $m_Pass = "Your PASSWORD here"; $m_Db = "Your DB NAME here"; //Code for connection: $connect = mysql_connect($m_Host,$m_User,$m_Pass) or die(mysql_error()); echo '<b style="color:#298A08;">Connected to MYSQL!</b>'; //Code for selecting db: mysql_select_db($m_Db,$connect) or die(mysql_error()); ?>
You're going to use these 2 functions in my next tutorials so
I hope you master now the mysql_connect function and mysql_select_db function!
Any questions / errors? Reply or PM !
Attached Files
Edited by Alexander, 16 August 2011 - 04:36 PM.


Sign In
Create Account



Back to top










