How to Connect to MySQL Using PHP

Create a new PHP file on your web server, and open it in your favorite text editor., Start your file by typing the open and close PHP tags with some space in between to work with. , Type this on a new line.

4 Steps 1 min read Easy

Step-by-Step Guide

  1. Step 1: Create a new PHP file on your web server

    This line of code creates the foundation for the communication with the MySQL server.

    The function "mysql_connect" takes a minimum of 3 string arguments.

    The first is the IP address or domain name of your server; you should change this from localhost to the address of your MySQL server.

    The second argument is the MySQL user that we will authenticate, and the third argument is the password for our MySQL user (in this case I left the password blank).

    The output of "mysql_connect" is a resource datatype, and in the code above we assigned it to a variable ($con) so we could use it later.

    For more information on mysql_connect(), visit the PHP documentation.
  2. Step 2: and open it in your favorite text editor.

  3. Step 3: Start your file by typing the open and close PHP tags with some space in between to work with.

  4. Step 4: Type this on a new line.

Detailed Guide

This line of code creates the foundation for the communication with the MySQL server.

The function "mysql_connect" takes a minimum of 3 string arguments.

The first is the IP address or domain name of your server; you should change this from localhost to the address of your MySQL server.

The second argument is the MySQL user that we will authenticate, and the third argument is the password for our MySQL user (in this case I left the password blank).

The output of "mysql_connect" is a resource datatype, and in the code above we assigned it to a variable ($con) so we could use it later.

For more information on mysql_connect(), visit the PHP documentation.

About the Author

D

Daniel Howard

Daniel Howard specializes in education and learning and has been creating helpful content for over 8 years. Daniel is committed to helping readers learn new skills and improve their lives.

45 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: