Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Connecting to MYSQL Database!

mysql

  • Please log in to reply
9 replies to this topic

#1 Epatron

Epatron

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 11 August 2011 - 08:39 AM


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.

  • 4

#2 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 11 August 2011 - 08:46 AM

Ive wanted to learn Php for a while, maybe ill learn it someday too. XD

Nice tut, good luck. :)
~ Committed.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#3 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 13 August 2011 - 10:32 AM

Wait, pictures count as screenshots?
  • 0

#4 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 13 August 2011 - 01:35 PM

Wait, pictures count as screenshots?


He'll probably need to put up an output screenshot.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#5 Epatron

Epatron

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 13 August 2011 - 01:36 PM

Wait Do I? :ss
  • 0

#6 Roger

Roger

    Skadoosh!

  • Administrator
  • 1222 posts
  • Programming Language:C, PHP
  • Learning:Others

Posted 13 August 2011 - 01:46 PM

If you can, you should, especially since you're doing php stuff.. just a screenshot would be good.
  • 0

New around here? Click here to register and start participating in under a minute?

Or do a quick search and you may find the answer you're looking for.


#7 Epatron

Epatron

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 54 posts

Posted 13 August 2011 - 01:47 PM

I'll add 2morrow, going bed now ^^!
  • 0

#8 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 16 August 2011 - 10:14 AM

Good tutorial! Very simple and straight forward. I'm looking forward to seeing some more advanced tutorials from you.
  • 0

#9 GuageCage

GuageCage

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Learning:Java, C++, PHP, (Visual) Basic, JavaScript, PL/SQL, Visual Basic .NET, Bash

Posted 19 June 2012 - 10:26 AM

Thanks. i will follow your remaining tutorials. I appreciate it. :cool:
  • 0

#10 lui0322

lui0322

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C, C++, C#, PHP, (Visual) Basic
  • Learning:PHP, JavaScript, ActionScript, Others

Posted 04 November 2012 - 08:17 AM

hmmmm i want to learn php
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download