eHow launches Android app: Get the best of eHow on the go.

How To

How to Connect to a MySQL Database with PHP

Member
By Mike
User-Submitted Article
(1 Ratings)

This tutorial assumes that you have been exposed to both the MySQL database and some PHP programming.

MySQL is a very popular relational database used on the internet for millions of websites. It is fast, easy to use and portable over a variety of platforms.

PHP is programming language that allows website developers to create dynamic sites for a variety of things.

Combining these two seems like a "no-brainer". Here is how you connect to MySQL through PHP.

Difficulty: Easy
Instructions

Things You'll Need:

  • a MySQL database
  1. Step 1

    Launch notepad or some other text writing software that you use to create your PHP files. I use notepad since it is free. A PHP file is nothing more than a text file.

  2. Step 2

    First connect to your database by writing the following line in your PHP file:

    mysql_connect("localhost", {username}, {password}) or die(mysql_error());

    Replace {username} and {password} with the username and password for your database. This says connect to MySQL with the {username} and {password} being provided on the machine called "localhost". If it fails return the error.

  3. Step 3

    Now select the database you actually want to connect to with this code:

    mysql_select_db({database name}) or die(mysql_error());

    Replace {database name} with the actual name of your database. This line says to connect to the database you have provided with the credentials you submitted in the previous step. If it does not connect, return the error being reported.

Comments  

mpodlesny said

Flag This Comment

on 6/22/2009 thanks...it only looks complicated..:)

kimmij said

Flag This Comment

on 6/22/2009 I will never be as smart as you about this stuff. Impressive! Great article on How to Connect to a MySQL Database with PHP. 5 *'s and recommended.

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics