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.
Step-by-Step Guide
-
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. -
Step 2: and open it in your favorite text editor.
-
Step 3: Start your file by typing the open and close PHP tags with some space in between to work with.
-
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
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.
Rate This Guide
How helpful was this guide? Click to rate: