How to Learn PHP & MySQL
Both PHP and MySQL are technologies that you can teach yourself. Lots of training books and websites exist with excellent tutorials on these topics, so you can find plenty of resources to build a good foundation with PHP programming in conjunction with MySQL database systems. You can acquire the skills to build a basic PHP and MySQL website very quickly However, if you are planning on developing substantial applications in the long term, give yourself a thorough grasp of the technologies involved, rather than just learning enough to get by.
Instructions
-
-
1
Familiarize yourself with database concepts. If you are new to database development, it's vital to understand the basic ideas involved in systems such as MySQL. MySQL is a relational database management system. Entity Relationship modeling is often used with MySQL databases, through which a set of data is defined as a number of entities with set relationships. For example, a customer order database could have an entity, and therefore table, representing customers, with their names and contact details stored within it. The same database could have an order table, with one customer potentially related to one or more orders, but each order related to only one customer.
-
2
Find out about basic SQL and database building. If you have a Web server with an installation of MySQL on it, log in to the system and browse around. In most cases database systems are built using SQL code, but if you have tools such as phpMyAdmin you can create a database using a graphical user interface. Even if you do not build your database in SQL statements, you will need them to query it, so learn the basic syntax. The following is an example of SQL code creating a database table:
CREATE TABLE Customer (
Cust_ID int,
CustName varchar(255),
CustAddress varchar(255) )
-
-
3
Gain an understanding of what happens on the server when a database-driven website is viewed by users. PHP is a server-side programming language, which means that it runs on the Web server. When a user visits a page on a PHP and MySQL website, their browser sends a request for the page to the Web server. The PHP script requested executes all required processes, including connecting to the MySQL database and querying it for data. The script then builds this data into HTML structures and sends these to the user's browser in the form of a Web page. Understanding this process is vital when learning PHP and MySQL development.
-
4
Learn PHP syntax. If you have only used markup languages such as HTML in the past, PHP programming requires an adjustment in your approach. When a PHP script is requested, it runs as a program, writing out HTML as it goes. The following example PHP script creates an HTML page and writes data out to it:
<html>
<head></head>
<body>
<?php
$today = date("m/d/y");
echo "<p>Welcome! Today is ".$today."</p>";
?>
</body>
</html>
This script creates an HTML page, carrying out processing and writing the results into the page body. Today's date is read into a variable, then this variable is included within an HTML paragraph along with some text. If you copy this into a file saved with ".php" extension, upload it to your server and browse to it, you will see the paragraph within the page body, but not the PHP code, as this is only used on the server.
-
5
Connect your PHP scripts to your MySQL databases. Once you have a clear idea of how the various elements in a PHP and MySQL website work, you then need to understand how to connect them. The following example PHP code shows how to connect to a database on the same server, providing the database name, username and password:
mysql_connect("localhost", "myusername", "mypassword");
mysql_select_db("mydatabase");
Your own scripts should be altered to suit the details of your database. Once connected, you can query the database as follows, writing the results in HTML:
$query = "SELECT * from Customer";
$result = mysql_query($query);
echo "Customer names:<br/>";
while($row = mysql_fetch_array($result)) {
echo $row['CustName']."<br/>";
}
This code gets all data from the Customer table, writing out the customer names with HTML line breaks between them.
-
1
Tips & Warnings
Get a solid foundation for future development. Take the time to learn PHP and MySQL at a conceptual level before actually starting to program.
Although you do primarily learn any development skill by doing it, skipping over the conceptual stage is likely to produce inefficient websites and ultimately lead to frustration.
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images