Web Design Forum: TUTORIAL: Connecting to a mysql database - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: Connecting to a mysql database Rate Topic: -----

#1 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 26 September 2009 - 03:42 AM

Ok so this might seem like an easy task for most php programmers but for beginners who are just starting out might not be able to sucessfully connect to a mysql database so i decided to write this short tutorial on it

Mysql_connect what it does

Ok so the mysql_connect function is pretty much the bread and butter of connecting to the database so first imma give you 3 simple things to remember when you look at this function.

1.Host
2.User
3.Password
Ok so let me explain these 3 things for you real fast Host is normally 1 of 2 things localhost or mysql on my site its mysql if ur using a testing server most always it will be localhost. Ok so lets move onto User this is the username you use to sign into phpmyadmin and password is the password you use to sign into phpmyadmin.
so in the end the connect function should look something like this


mysql_connect(localhost,user,'pass') or die ("Could not connect to database");

Oh and btw the
or die
statement is only called if for some reason your site can not connect to the database then it will output Could not connect to database

Mysql_select_db Very easy one

Ok so this function is basically just the name of your database so heres the example


mysql_select_db(mydatabase);


Lets put it all together

$host = "localhost";
$user = "user";
$pass = "pass";
$db = "database name";
mysql_connect($host,$user,$pass) or die ("Could not connect to the database");
mysql_select_db($db);


Ok so thanks for reading and i hope beginners in php have took away something useful from this tutorial :yahoo:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users