Wednesday, April 23, 2014

PHP and HTML Technique 1


Thus far we have only looked at PHP as a standalone code. However, one of the main reasons PHP is useful as a web programming language  is that it can be blended with html. This allows for hyrbid code that can take the results of a method or function, and display it in a web browser

It is important to keep in mind the differences between the two languages, as doing so will help you  know what to change or modify when it comes time to edit your webpage. As a reminder, HTML is a markup language, and should only be used to render  and  format text and media. PHP is a programming language that should be used to make decisions on  what text and media the html will display,. The two languages complement each other, and neither  should be used as a replacement for the other.

With that in mid, there are two methods to go about blending the two languages. The first is to write have a HTML code with pieces of inserted within it. 
<html> 
 <title>HTML and PHP</title>
 <body>
 <h1>Best Friends for life</h1>

 <?php
 //PHP CODE
?>

 <b>HTML <3 PHP</b>

 <?php
 //MORE PHP CODE
 ?>
 </body>
 </html>

Notice that the PHP code is completely separated by start and close PHP tags, and there exists no HTML within the PHP. However, the HTML tags are not concerned with excluding PHP. 

In other words, there is no HTML in the PHP. But there's PHP in the HTML. 

This is the preferred method for integrating the two languages, especially for content heavy and complicated web pages. However, there is still a place for the integration technique demonstrated in the next blog post.





Monday, April 21, 2014

PHP and MySQL Databases II

Once you open your MySQL database in your PHP code, it is ready to be queried for data. When retrieving data from a database, it is good practice to put the results of the query in a variable. This allows the for the data to be referenced at a later point in code, after the database has been closed.

Assuming that you have already opened your database, retrieving data from it is only a statement away.
The retrieval statement takes twos parameters:
1. the connection statement that you used to open the database
2. the query statement itself

Since MySQL databases must be queried using SQL, parameter number 2 needs to be an SQL statement,

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

$myresult = mysqli_query($mycon,"SELECT * FROM cities");


The above statement takes all of the data in the "cities" table of "my_db",
and stores it into a variable called myresult. 

As mentioned earlier, long after the database connection has been closed, the information in the "cities" table can still be used through referencing the myresult variable.

Deleting information dynamically, through PHP code, works in a similar fashion. The select statement is replaced with a delete  statement. 

mysqli_query($con,"DELETE * FROM cities");

The above statement will remove all of the data in the "cities" table of "my_db".
Since data is being deleted and not retrieved, there is no need to store the results of the statement. in a local variable.


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);