Print

SiteGround Tutorials


PHP Tutorial

What is PHP?

PHP was at first created as a simple scripting platform called "Personal Home Page". Nowadays PHP (the short for Hypertext Pre-Processor) is an alternative to Microsoft's Active Server Page (ASP) technology. It is an open source server-side language, which is used for creating dynamic web pages, which will be embedded in HTML. PHP is usually used in conjunction with a MySQL database on Linux/UNIX web servers. It is one of the most popular scripting languages among today's web developers.

A unique feature of PHP is that it can also be embedded within those HTML pages that are saved with a .php file extension, allowing you to change the content of many web pages at a time by simply modifying one .php file. A server-side PHP engine transforms all PHP sections into HTML and the visitor does not see any of the PHP script.

PHP is a widely-used general-purpose scripting language and interpreter that is freely available. A full explanation of all the PHP tags, complete user manual and lots of tutorials can be found on PHP's official page.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

MySQL Tutorial

What is MySQL?

MySQL is a freely available open source Relational Database Management System [RDBMS], a database engine of sorts that uses Structured Query Language (SQL). SQL is the most popular language for adding, accessing, and processing data in a database, and is most noted for its rapid processing, proven reliability, and ease and flexibility of use. Best mate for PHP, MySQL is part in almost every open source application. Good examples for use of its power are phpBB, osCommerce, and phpNuke.

You can see how dynamic MySQL can be when used together with PHP on the web site of the hottest web2.0 applications digg.com and del.icio.us. You can search with 1 keyword or a keyword phrase, the returned results are sorted and best matched through the combined use of PHP and MySQL, generating a dynamically created web page on the fly. Our own site uses MySQL, which manages to answer more than 120 queries per second! Can you do that?

MySQL can be used on multiple web servers, including Windows and Linux and it is offered in all the SiteGround's packages free of charge.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

MySQL Tutorial

Creating MySQL Database and User

Step 1: Creating a MySQL Database

Creating a MySQL database with the cPanel, which is included in all of the SiteGround's Linux hosting plans is quite an easy task. In order to create a MySQL database on your hosting account you need to log in your cPanel. This can be done by either using the "Go!" button in the "Manage Accounts" section of the customer's area as shown in the image below:

Or by using one of the following URLs:

yourdomain.com/cpanel
cpanel.servername.com

Where servername stands for the server that hosts your account.

Once in the cPanel you should go to the "MySQL Databases" section by clicking on its icon . When empty, the section looks like this:

To create the database, you need to fill a name in the field an click on the "Create Database" button:

This will lead you to a page, which confirms that the database creation was successful.

Important You need to create at least one database user and grant that user permission to use the new database. Refer to Creating a MySQL user and Grant a user's permissions to a MySQL database for more information.

Step 2: Creating a MySQL user

Click on the "Go Back" link to continue with the creation of a user, who will be able to connect to the newly created database. Creating a user is much like any other registration. You just need to fill in the desired user name and password like shown in the image below:

And press the Create User button. Again a confirmation page is loaded.

Important Creating a user does not automatically allow the user to use a database - you need to grant that user permission to the specific database first. Refer to Grant a user's permissions to a MySQL database for more information.

Step 3: Add Users To Your Databases

Click on the "Go Back" link to continue setting up the database. In order for the user to be able to access the database he needs to be added to it. This is done from the last section "Add Users To Your Databases". Select the user from the drop-down menu on the left, and the database name from the drop-down menu on the right. The "All" box is checked by default, so leave it as it is, just click on the "Add User to Database" button. Again a confirmation page is loaded. Click on the "Go Back" link and you will see that a set of Connection Strings have appeared just below the database name. This means that all the steps have been successfully completed.

Important The user will only have permission to change things you have given them the privileges for. Make sure the user has privileges for everything they need to modify.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

MySQL Tutorial: Removing MySQL Database and User

Removing a MySQL database

Since most accounts have MySQL database creation limits, it is recommended to remove any unnecessary MySQL databases so that you are able to create more databases when necessary.

Step 1: To access the MySQL Account Maintenance Menu, click on the icon above the words MySQL Databases on the main screen of your cPanel interface.

Step 2: Click on "Delete" next to the name of the database you wish to delete.

Important Before deleting a database due to problems with that database, try using on the database to repair any problems with it.

Removing a MySQL user

When a MySQL user is no longer deleted, you can remove them to prevent them from having access to any databases.

Step 1: To access the MySQL Account Maintenance Menu, click on the icon above the words MySQL Databases on the main screen of your cPanel interface.

Step 2: Scroll down to the user's name under Users:

Step 3: Click on "Delete" next to the user's name to remove their MySQL user.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

MySQL Tutorial

How to Access and Manage your MySQL Database - phpMyAdmin

Now there are two ways to access and manage your newly created database. The first one is locally trough the Web based manager - phpMyAdmin, which is accessed trough the cPanel's "MySQL Databases" section. The phpMyAdmin link is at the bottom of the page.

A new page/tab of your browser will open and the phpMyAdmin will load there and looks like this:

With it, you can export (dump) one or more of your databases, import a dump file, change the collation of your databases and execute SQL queries.

The phpMyAdmin menu appears in a separate browser window. To return to cPanel, simply close that browser window.

Remote Connection

The second way to connect to your databases is remotely. To do so, you should add your IP address in the "Access Hosts" section in the MySQL page. For example if your address is 1.2.3.4 you need to type it in the box and click on the "Add Host" button like shown in the image below:

As before, this will lead you to a page where the cPanel confirms the successful adding of the host. A group of hosts can also be added by using the "%" symbol. For example "%.domain.com" will include ns1.domain.com, server1.domain.com, test.domain.com, etc. The second thing you need to connect to your database remotely is software that can do that. Almost all of the SQL management applications are capable of connecting to a remote database. Some of the most popular are: CuteSQL, myAdmin etc.

Click here to learn how to backup/restore your MySQL database



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

MySQL Tutorial

MySQL Export: How to backup your MySQL database?

You can easily make a dump of (export/backup) your MySQL database via the cPanel > MySQL Databases > phpMyAdmin. The shortcut to the phpMyAdmin tool is located at the very bottom of the MySQL Databases tab.

Once the database administration tool loads, please select from the drop down menu called Database (located in the left upper corner of your web browser) the database that your application uses.

After the page is fully loaded, please navigate to the [Export] button:

Click on the Select all tool and please check the Add DROP TABLE box.

Finally check the Save to file check box and click on [Go] and the database dump will be downloaded to your computer.

MySQL Import: How to restore your MySQL database?

To restore (import) a database in phpMyAdmin, first choose the database you'll be restoring from the left menu. Then click the SQL tab. You have the option of importing an .sql file. Use the [Browse] button to find it on your local computer and then click Go at the bottom of the page.

You might want to check the cPanel backup tutorial for more information about your website backup.

You may also request a backup creation of your whole account by posting a ticket from the Exclusive Professional Services tab inside your Help Desk area.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

PHP/MySQL Tutorial - PHP MySQL Connection

Step 1:

PHPinfo

One of the most basic PHP tags is the PHPinfo. Open just any text editor (like notepad) and type in the following:

<?
phpinfo();
?>

And save it as public_html

Now upload this to your public_html directory and go to it in your browser using the following link:

http://yourdomain.com/phpinfo.php

You will see a huge page with all the details of the PHP installation on it, scroll down through all this information and you will see a section where the MySQL details are fitted.

Before you can do anything with your database, you must create a table. A table is a section of the database for storing related information. In a table you will set up the different fields which will be used in that table. Because of this construction, nearly all of a site's database needs can be satisfied using just one database.

Creating a table in PHPMyAdmin is simple, just type the name, select the number of fields and click the button. You will then be taken to a setup screen where you must create the fields for the database. If you are using a PHP script to create your database, the whole creation and setup will be done in one command.

Creating a table in PHP is slightly more difficult than with MySQL. It takes the following format:

CREATE TABLE tablename {

Fields

}

The fields are defined as follows:

fieldname type(length) extra info,

The final field entered should not have a comma after it.

Step 2: Connect to the Database
Step 3: Execute commands on the server
Step 4: Display table data
Step 5: Select individual records



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

PHP/MySQL Tutorial - PHP MySQL Connection

Step 2:

How to Connect to Your Database

The first thing you must do before you can do any work at all is to connect to the MySQL database. This is an extremely important step as, if you are not connected, your commands to the database will fail.

Good practice for using databases is to specify the username, password and database name first so that if you change any of them at a later date you will only have to change one line:

$username="username";
$password="password";
$database="your_database";

At this point you may be wondering if it is a security risk, keeping your password in the file. You don't need to worry, though, because the PHP source code is processed by the server before being sent to the browser so it is impossible for the user to see the script's source.

Next, you will need to issue the command to start a database connection:

mysql_connect(localhost,$username,$password);

This line tells PHP to connect to the MySQL database server at 'localhost' (localhost means the server that the site is running one. Another vital command is:

mysql_close();

This is a very important command as it closes the connection to the database server. Your script will still run if you do not include this command but too many open MySQL connections can cause problems for your account. It is good practice to always include this line once you have issued all your commands to the database, to keep the server running well.

After you have connected to the database server you must then select the database you wish to use. This must be a database to which your username has access. The following command:

@mysql_select_db($database) or die( "Unable to select database");

is used to do this. This tells PHP to select the database stored in the variable $database (which you have set earlier). If it cannot connect it will stop executing the script and output the text:

Unable to select database

This extra 'or die' part is good to leave in as it provides a little error control but it is not essential.

Now you have connected to the server and selected the database you want to work with, you can begin executing commands on the server.

Step 3: Execute commands on the server
Step 4: Display table data
Step 5: Select individual records



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

PHP/MySQL Tutorial - PHP MySQL Connection

Step 3:

How to Execute Server Commands

There are two ways of executing a command. One is to just enter the command in PHP. This way is used if there will be no results from the operation.

The other way is to define the command as a variable. This will set the variable with the results of the operation.

In this part of the tutorial we will use the first way as we are not expecting a response from the database. The command will look like this:

mysql_query($query);

The useful thing about using this form of the command is that you can just repeat the same command over and over again without learning new ones. All you need to do is to change the variable.

Here is the complete code that should be used to create a MySQL table in PHP:

<?
$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE tablename(id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,field1-name varchar(20) NOT NULL,fiels1-name varchar(20) NOT NULL,field3-name varchar(20) NOT NULL,field4-name varchar(30) NOT NULL,field5-name varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
?>

Enter your database, MySQL username and MySQL password in the appropriate positions on the first three lines above.

The next query should fill in the table. Here is a sample one:

$query = "INSERT INTO tablename VALUES
('','$field1-name','$field2-name','$field3-name','$field4-name','$field5-name')";

You can't insert more values than the number of fields you have created with the previous query.

Step 4: Display table data
Step 5: Select individual records



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

PHP/MySQL Tutorial - PHP MySQL Connection

Step 4:

How to Display MySQL Table Data?

After you have created the table and filled it with data, you will probably need to display it. This is usually done using basic HTML code. Putting the data using HTML pages is almost identical to inserting it using a PHP script. The benefit, though, is that you do not need to change the script for each piece of data you want to input and you can also allow your users to input their own data.

The following code inserted in an HTML page will display the data from the databases with textboxes in which the appropriate details are fitted:

<form action="insert.php" method="post">
Value1: <input type="text" name="field1-name"><br>
Value2: <input type="text" name="field2-name"><br>
Value3: <input type="text" name="field3-name"><br>
Value4: <input type="text" name="field4-name"><br>
Value5: <input type="text" name="field5-name"><br>
<input type="Submit">
</form>

The next thing you need is a new PHP script, which instead of filling in the database with data, will get the data and display it.

<?
$username="username";
$password="password";
$database="your_database";

$field1-name=$_POST['Value1'];
$field2-name=$_POST['Value2'];
$field3-name=$_POST['Value3'];
$field4-name=$_POST['Value4'];
$field5-name=$_POST['Value5'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO tablename VALUES
('','$field1-name','$field2-name','$field3-name','$field4-name','$field5-name')" ;mysql_query($query);

mysql_close();
?>

This script should then be saved as insert.php so that it can be called by the HTML form. It works because, instead of the data being entered locally, it is being entered into the form and stored in variables which are then passed to the PHP.

Now that you have at least one record, if not many more, in your database you will want to know how you can output this data using PHP.

The first command you will need to use is a MySQL query made up like this:

SELECT * FROM tablename

This is a basic MySQL command which will tell the script to select all the records in the tablename table. Because there will be output from this command it must be executed with the results being assigned to a variable:

$query="SELECT * FROM tablename";
$result=mysql_query($query);

In this case the whole contents of the database is now contained in a special array with the name $result. Before you can output this data you must change each piece into a separate variable. There are two stages to this.

The first one is counting the rows. Before you can go through the data in your result variable, you must know how many database rows there are. You could, of course, just type this into your code but it is not a very good solution as the whole script would need to be changed every time a new row was added. Instead you can use the command:

$num=mysql_numrows($result);

This will set the value of $num to be the number of rows stored in $result (the output you got from the database). This can then be used in a loop to get all the data and output it on the screen.

The second stage is to set up the loop. You must now set up a loop to take each row of the result and print out the data held there. By using $num, which you created above, you can loop through all the rows quite easily. In the code below, $i is the number of times the loop has run and is used to make sure the loop stops at the end of the results so there are no errors.

$i=0;
while ($i < $num) {

CODE

$i++;
}

This is a basic PHP loop and will execute the code the correct number of times. Each time $i will be one greater than the time before. This is useful, as $i can be used to tell the script which line of the results should be read. As the first line in MySQL output is 0, this will work correctly.

The final part of this output script is to assign each piece of data to its own variable. The following code is used to do this:

$variable=mysql_result($result,$i,"fieldname");

So to take each individual piece of data in our database we would use the following:

$field1-name=mysql_result($result,$i,"field1-name");
$field2-name=mysql_result($result,$i,"field2-name");
$field3-name=mysql_result($result,$i,"field3-name");
$field4-name=mysql_result($result,$i,"field4-name");
$field5-name=mysql_result($result,$i,"field5-name");

You do not need to get the ID field because there is no use for it in the output page.

You can now write a full script to output the data. In this script the data is not formatted when it is output:

<?
$username="username";
$password="password";
$database="your_database";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tablename";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$field1-name=mysql_result($result,$i,"field1-name");
$field2-name=mysql_result($result,$i,"field2-name");
$field3-name=mysql_result($result,$i,"field3-name");
$field4-name=mysql_result($result,$i,"field4-name");
$field5-name=mysql_result($result,$i,"field5-name");

echo "<b>$field1-name
$field2-name2</b><br>$field3-name<br>$field4-name<br>$field5-name<hr><br>";

$i++;
}

?>

This outputs a list of all the Values stored in the database. This just gave you a very basic output, though and is not particularly useful for a working website. Instead, it would be better if you could format it into a table and display it like this. Doing this formatting is not particularly complicated. All you need to do is use PHP to output HTML and include your variables in the correct spaces. The easiest way to do this is by closing your PHP tag and entering the HTML normally. When you reach a variable position, include it as follows:

<? echo $variablename; ?>

in the correct position in your code.

You can also use the PHP loop to repeat the appropriate code and include it as part of a larger table. For example:

<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Value1</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value2</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value3</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value4</font></th>
<th><font face="Arial, Helvetica, sans-serif">Value5</font></th>
</tr>

<?
$i=0;
while ($i < $num) {

$field1-name=mysql_result($result,$i,"field1-name");
$field2-name=mysql_result($result,$i,"field2-name");
$field3-name=mysql_result($result,$i,"field3-name");
$field4-name=mysql_result($result,$i,"field4-name");
$field5-name=mysql_result($result,$i,"field5-name");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $field1-name; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $field2-name; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $field3-name; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $field4-name; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $field5-name; ?></font></td>
</tr>

<?
$i++;
}

echo "</table>";

This code will print out table headers, and then add an extra row for each record in the database, formatting the data as it is output.

Step 5: Select individual records



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

PHP/MySQL Tutorial - PHP MySQL Connection

Step 5:

How to Select Individual Records

As well as showing the whole database, PHP can be used to select individual records, or records which match certain criteria. To do this you must use a variation of the SELECT query. To display the whole table you used the query:

SELECT * FROM tablename

If we just wanted to select ones which have value=1 in the field1-name row you would use the following query:

SELECT * FROM tablename WHERE fiels1-name='1'

As with other MySQL queries, it is almost like plain English text. In the same way you could select records based on any field in the database. You can also select ones with more than one field by adding more:

field='value'

sections onto the query.

For further reference you can visit the official websites of PHP and MySQL.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved
Previous Next

Zend Optimizer Tutorial

Zend Optimizer is available on SiteGround Servers!

What is Zend Optimizer?

Zend Optimizer is a free application that runs the files encoded by the Zend Guard. Zend Optimizer greatly enhances the performance of PHP applications. Generally this is the main use of the Zend Optimizer software.

The Zend Optimizer goes over the code generated by the standard Zend run-time compiler and optimizes it for faster execution. In addition, the Zend Optimizer enables PHP to load and execute files encoded by using Zend Guard. The standard Zend run-time compiler used by PHP is indeed extremely fast, generating code that is usually 2 to 10 times faster. But an application that uses the Zend Optimizer, most commonly executes the scripts another 40% to 100% faster.

System requirements:

Supported Operating Systems, Platforms and OS versions:

  • Linux x86
  • Linux x86-64
  • Linux Power
  • Solaris Sparc 8, 9, 10
  • Solaris x86 9, 10
  • FreeBSD x86 5.x, 6.x
  • Windows x86 2000, XP, 2003
  • OS X Mac Power
  • AIX pSeries 5

Supported Web Servers:

  • Apache 1.3.x
  • Apache 2.0.x (Prefork mode only)
  • IIS 5, 6

PHP Compatibility

Supported PHP versions:

  • 4.2.x up to 4.4.x
  • 5.0.x, 5.1.x

OS X Mac supports PHP versions:

  • 4.3.x up through 5.1.x

Zend Guard Compatibility:

Files encoded with the Zend Guard must be compiled with a compatible version of Zend Guard. When in doubt always use the latest version of Zend Optimizer. If the versions of Zend Guard and Zend Optimizer are incompatible, the Optimizer will fail to run encoded files and will register an error message.

Interesting details:

Always bear in mind that Zend Optimizer will *not* run if the following PHP settings are enabled:

1. Thread Safety
2. Enable Versioning

To ensure that Zend Optimizer is properly running create a .php file with the following code:

<?php
phpinfo();
?>

Once this is done access the file via web browser. The part associated with Zend Optimizer will look like this:

This program makes use of the Zend scripting language engine: Zend Engine vX.Y.Z, Copyright (c) 1998-2006 by Zend Technologies Ltd. with Zend Optimizer vX.Y.Z, Copyright (c) 1998-2006 by Zend Technologies Ltd.

The downloaded package includes an "Accelerated by Zend Optimizer" icon which you can post on your site, thus letting users to know you are running Zend Optimizer.

The Zend Optimizer is capable of performing around 20 different optimizations, some of which are quite complex. One of the simpler optimizations that the Zend Optimizer does is to change post-incrementing to pre-incrementing, where possible, since pre-incrementing is the faster operation of the two. You can adjust your Zend Optimizer to perform only the desired optimizations. Each optimization can be turned on or off by setting the corresponding option. Currently the latest Zend Optimizer versions come with some of the most complex optimization options disabled as it is considered that way for best performance.



PHP Hosting
(c) Copyright 2006 SiteGround Web Hosting Provider. All rights reserved