How to Use MySQL with PHP
Do you know how to use MySQL with PHP? If you don’t, then, here is your chance to learn something new! Even before you venture into how to use MySQL with PHP, you must have known how to use the two separately.
Why is MySQL is combined with PHP? For one, MySQL has been known as the most popular Open Source database system while PHP is the most popular server side scripting language. On its own, MySQL is used for database creation, manipulation and management. PHP on the other side is a programming language that can be used to present how MySQL databases are handled over the web servers. Here are some uses of a combination of PHP with MySQL:
– Web Graphics management: When graphics such as pictures are stored in a MySQL database and they need to be called within a web program, PHP as a server side scripting language is used to initiate the MySQL database management.
– Forums management: There are thousands of forums online today which use MySQL database system and PHP scripting language. With this combination, it is easy to edit, delete, update and keep thercords for the forums in a database.
Connecting MySQL with PHP
For the two to be used together, they have to be connected together. Here are some facts you have to know when it comes to connecting MySQL with PHP:
- You have to choose a web server: The most common server often used for the manipulation of the two is the Linux server. However, some people often use the windows server while at other times, there are those who prefer manually configuring PHP and MySQL on their preferred choice of server.
- Performing Installations: Once you have a server, you will have to install both PHP and MySQL on your server. Both are free to download though you may need additional information on how to configure.
- Managing MySQL and PHP: There are two ways in which you can connect MySQL with PHP. One of this ways is to use PHP scripts. Another way of connecting MySQL with PHP is through the installation of PHPMyAdmin onto your server. PHPMyAdmin gives you an administrative interface through which you can edit, add, remove, backup and view your MySQL databases.
If you want to connect PHP directly with MySQL without using the PHPMyAdmin interface, here is an example code that you could use once your server is set up and functioning:
1. Creating a Connection with MySQL: Connect To the MySQL database using PHP: When performing this action, we use the mysql_connect() function. Here is how it is done:
<?php $con = mysql_connect("localhost","username","password"); if (!$con) die('Could not connect to the server: ' . mysql_error()); ?>
Note: Remember to replace the words “Username” and “Password” with your MySQL database username and password.
2. Managing Your MySQL Database: Once you have an active connection to the database, you can add, edit, insert, remove and manipulate your MySQL database as you please. For instance, for you to add data after the connection is active, here is an example of how you will do it:
<?php $con = mysql_connect("localhost","username","password"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("your_database", $con); mysql_query("INSERT INTO Students (FirstName, LastName, Age) VALUES ('Victor', 'Mildred',25)"); mysql_query("INSERT INTO Students (FirstName, LastName, Age) VALUES ('Benter', 'Sam',30)"); mysql_close($con); ?>
3. Closing A Connection: Once you are done with working in your database, you will have to close your database connection. Again, the PhP function used to close connections into MySQL is mysql_close(); this can be used as indicated in the code above.
There are many other ways that MySQL and PHP can be used alongside each other. Whoever, these are the most basic principles of using MySQL with PHP.