Mcloide's resources library

All about PHP, Javascript, concepts and development

Posts Tagged ‘mysql’

PHP Basics Series – Starting with PHP

Posted by mcloide on June 30, 2009

Hi and welcome to the second article of the PHP Basics Series. The first article – PHP Basics series – coding – was about coding best practices, standards and methods. A small overview on what you need to know to be a good coder.

In this second article we are going to talk about the very beginning of PHP, how it works, how to start coding on it and a bit of history.

PHP stands for Hypertext Preprocessor and it was created based on the C, Java and Perl (used until today on extra modules) languages. With many authors, editors, contributors, etc, is hard to name one as it’s creators, therefore you can point Rasmus Ledorf (who wrote the original gateway binaries) and Andi Gutmans and Zeef Suraski (both responsible for witting the parser that formed PHP3) as the original creators of PHP3.

If we had to create a in-line structure to show how it works, it would be something like this:

The client (your browser) requests a page to the server. The server reads the page and see if there are any PHP script and it will parse that script generating the correct result that can be embed to the HTML / XML / Javascript (JS) / etc.

Off course there is much more when we are talking about the way that PHP works, and it’s history, but I will focus more on the coding techniques.

Before we create our first code and I promise that it will not be “Hello World“, you will need to have PHP installed on your development machine or in a server where you can store your PHP files to be executed.

If you are on a Windows machine you can either install PHP manually adding it to IIS server or you can install a PHP bundle that will install for you the Apache server, PHP engine and MySQL (if you desire to, otherwise you can use PHP native SQLite).

Truly I’m not the best indicated to show how to install PHP on a IIS server, but there is a very good step by step – Vista – on Bills IIS Blog: How to install PHP on IIS.

Installing the bundle, for a development machine, at this point, is the most recommended. There is a great bundle, AppServ, which is pretty easy to install and configure. It will come with all of the most usual libraries that you need and you can also choose to install some extra ones.

In other hand, if you are on a Linux machine (Ubuntu, Xubuntu – love it, Suse, etc) most likely you already have PHP, Apache and MySQL installed. Therefore you can check by using the Synaptic Package Installer, and if is not there, install it. Just make sure that you have root permissions.

Now that you have PHP installed, let’s create our first code.

Note: If you don’t have an development editor, you can use Eclipse PDT. It’s a great editor and it’s free. Very similar to the Zend Studio. If you have any other editor that you like, such as Dreamweaver, Notepad, Edit+, etc, you can use it as well.

The first thing you need to know is how make the server to understand that it will be dealing with a PHP code. For that you will be using the PHP open / close tags. There are a set of 4 open / close tags for PHP, but we will focus on only one.

PHP holds as it’s open / close tags the following:

  1. <?php ?> – Full tag
  2. <? ?> – Short tags. As one of it’s variations you will have <?= that is the same as <? echo
  3. <% - ASP tags. More as a history point, but really, not useful at all.
  4. <script language=”php”></script> – besides the first one, the only one that you can trust to work.

While coding we could be using all of the 4 sets of PHP’s open / close tags, but, for keeping a standard and full compatibility on any PHP installation, we will always use the first set. The second set have some amazing features applied to it, but on the latest versions of PHP you need to enable it on the INI configuration and, if you are dealing with embending your code on a XML file you can have some parsing errors.

As I promised before, we are not going to write a “Hello World” application and we will code 2 types of application today. A standalone and a embed one. There is no real definition as standalone or embed types, but it will be easier, for now, to identify our files as standalone or embed. The standalone is a file that is purely PHP, no other code besides PHP. The embed is a HTML, XML, or any other type of file that have PHP code inside it.

Let’s take a look on a standalone file first: standalone.php (note the .php extension, whithout it, the server can’t recongnize that there is a PHP code inside it and it would only write the code on the screen as text).

standalone.php
<?php
$string    = ‘ This is a string and the number is: ‘;
$number = 7;
echo $string;
echo $number;

If you call the file from your browser (http://localhost/standalone.php) you will see the following result:

This is a string and the number is 7

As you may noticed by now, the close tag, ?>, for the above script was omitted. When you are dealing with only PHP code on a file, you don’t need to include the closing tag, the PHP engine will do that for you and it also is a simple performance and standard recomendation from PHP.

Among other uses from the standalone file, you can call it from the command prompt / command line: php /path/to/file/standalone.php. It’s results will be displayed on the command prompt screen. At first this does not seem too useful, but when getting more advanced, you will see that you can use this with cron jobs to increase your application power.

Let’s take a look now on the embed file, embed.php. Again the extension of the file must be .php otherwise the server will not understand that there is PHP code inside the file and it will not process it.

embed.php

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<title>
Page Title</title>
<!– other head data such as CSS styling and JS scripts –>

</head>
<body>
<p>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>

<?php
$string    = ‘ This is a string and the number is: ‘;
$number = 7;
echo $string;
echo $number;
?>
</p>
</body>
</html>

This is a good example of a PHP script that is embed into a XHTML file. When you call the file on your browser you will have the following result:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

This is a string and the number is 7

As you have notice, in the embed file the php close tag, ?>, is on place. Without it, the PHP engine would throw an error on the screen not letting the code to be parsed and the page to be displayed.

This is our first PHP application. For now on things will be increasing and we will focus a bit more on structures and tools that you can use while programming PHP.

For now on every time that we work with a PHP file that will be displayed on a browser or used on a cron job or for any other need, we will reference it as a application because that is what truly is.

See you on the next article, have fun.

Posted in PHP, development | Tagged: , , , , , , , , , , , , , | 1 Comment »

Using MySQL EXPLAIN to optimize queries

Posted by mcloide on June 24, 2009

MySQL is a powerful database and when used correctly you can easily get the most of it with little.

Using the query slow logs, query optimization, database optimization and some other techiniques is a great and reliable way to guarantee that your system can respond fast.

While developing, one great tool to use from MySQL is the EXPLAIN query. It returns, for a given query, the number of results, some other information, and the extra or missing indexes that you need.

Here is a brief demonstration of how the EXPLAIN query works:

EXPLAIN SELECT tt.TicketNumber, tt.TimeIn,
               tt.ProjectReference, tt.EstimatedShipDate,
               tt.ActualShipDate, tt.ClientID,
               tt.ServiceCodes, tt.RepetitiveID,
               tt.CurrentProcess, tt.CurrentDPPerson,
               tt.RecordVolume, tt.DPPrinted, et.COUNTRY,
               et_1.COUNTRY, do.CUSTNAME
        FROM tt, et, et AS et_1, do
        WHERE tt.SubmitTime IS NULL
          AND tt.ActualPC = et.EMPLOYID
          AND tt.AssignedPC = et_1.EMPLOYID
          AND tt.ClientID = do.CUSTNMBR;

Result:

table type possible_keys key  key_len ref  rows  Extra
et    ALL  PRIMARY       NULL NULL    NULL 74
do    ALL  PRIMARY       NULL NULL    NULL 2135
et_1  ALL  PRIMARY       NULL NULL    NULL 74
tt    ALL  AssignedPC,   NULL NULL    NULL 3872
           ClientID,
           ActualPC
      Range checked for each record (index map: 0x23)

In this case because ALL is the type for each table, MySQL will generate a combination of all rows to return in the query. In another words, this takes insane long time.

Anyway, for more optimization tips and the full manual of how

this works go to the MySQL Developer Articles.


Posted in mysql | Tagged: , , , | Leave a Comment »

Generate Data

Posted by mcloide on September 24, 2008

When you are designing a web application (or a website that does more than only showing content) you need to carefully consider scaling issues. So you have considered to have 100 users and now your application is falling a part because you are reaching 1000,000, well, that is definetly a scaling issue.

I’m not going to get too much inside the subject since you this article from Zend Developer Zone, Scaling Day-By-Day, takes good care of it.

Anyway, I want to point to you here is a tool that is going to help you with generating data to test your web application and the best of all, it is under GNU license and is very simple to use.

I’m putting this link in my resources page as well, but here it is: GenerateData.com check it out.

Posted in Ajax, PHP, Zend, development, mysql, resources | Tagged: , , , , , , , , , | Leave a Comment »

Mysql Handler Class – Version 2.0

Posted by mcloide on September 23, 2008

I have finally taken a better look at the class and I have rewrite it in some methods, but the biggest change was the complete adaptation for PHP4 or PHP5.

The class still with the same structure:

  • Constructor with a handler to initialize the mysql connection and database selection
  • select, insert, update, delete and query methods
  • pagination

As a suggestion, if you can, use the PHP5 version. PHP5’s object model is much stronger than PHP4.

Follows the links for downloading the class:

Posted in PHP, development, resources | Tagged: , , , , , | Leave a Comment »

MySQL Handler Class – Update

Posted by mcloide on September 22, 2008

I noticed that the class had a little mistake that would make it not work properly under PHP4 and a little annoying bug that I had on the select method. If you already had downloaded the class, please download this newer version.

Click here to download the newer version: Download

Posted in PHP, development, resources | Tagged: , , , , , , , | 4 Comments »

MySQL Handler Class

Posted by mcloide on September 4, 2008

Consider the scenario where you have only PHP4, you can’t use PEARL DBManager or even a Zend Framework. Using MySQLi extension from PHP is out of question and you can’t update the server because it will affect all other sites.

Well I got into a situation like that today and I had to modify all my MySQLi Handler class to work this around.

I have created a MySQL Handler class. It will work just like the MySQLi Handler class, but using mysql functions.

If you find yourself in a situation just like the one explained above, this might help.

I noticed that the class had a little mistake that would make it not work properly under PHP4 and a little annoying bug that I had on the select method. If you already had downloaded the class, please download this newer version.

Follow the links for downloading the class:

Posted in PHP, development, resources | Tagged: , , , , , | 17 Comments »