Jump to content

Connecting to MYSQL Database!

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts


Posted Image
Connect to MYSQL Database
Tutorial by: Epatron


  • Connect to MYSQL
    • Introductions
    • The PHP Code
    • Explanation

  • Selecting Database
    • The PHP Code
    • Explanation

  • Putting them together
    • The PHP Code
Connect to MYSQL
-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.


#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Ive wanted to learn Php for a while, maybe ill learn it someday too. XD

Nice tut, good luck. :)
~ Committed.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Wait, pictures count as screenshots?

#4
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python

RhetoricalRuvim said:

Wait, pictures count as screenshots?

He'll probably need to put up an output screenshot.
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#5
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Wait Do I? :ss

#6
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
  • Programming Language:C, PHP
  • Learning:Python
If you can, you should, especially since you're doing php stuff.. just a screenshot would be good.
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.

#7
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
I'll add 2morrow, going bed now ^^!

#8
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Good tutorial! Very simple and straight forward. I'm looking forward to seeing some more advanced tutorials from you.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users