Monday, April 21, 2014

PHP and MySQL Databases

Part of what makes programming in PHP so useful for web development is its compatibility with MySQL database.  If you are not already familiar, MySQL is a simple, open source database management system that allows a user to store large amounts of data in an organized fashion.

This tutorial assumes that you have  already created and saved your MySQL database, and are now preparing to retrieve data from it within your PHP code.


Opening your PHP database is the firs step. This is done through the MySQL connection command,
The command takes four parameters:
    • The host of the machine hosting the database. This can be a website or database,
    • The database username 
    • The database password
    • The database name.
Here is an example

$con=mysqli_connect("example.com","peter","abc123","my_db");

Once the database is opened, you can query it for the data you need. Specific details on MySQL querying will be covered in a future blog post. 

After you have completed and stored the retrieved information, it is important that you remember to close your connection to the database. This is important as it helps to conserve the host's resopurces, as well as prevent unauthorized access to the database. 

Here is an example of a close statement.

mysqli_close($con);




No comments:

Post a Comment