WebHQ

The Headquarters Of Web

Facebook Twitter RSS Feed Email

Install PHP and MySQL on Windows to create a local PHP MySQL dev environment

Published on: at 2:46 PM | Posted By:
Whether you are a seasoned PHP/MySQL programmer or a beginner, you will benefit from having a development environment. A development environment allows you to test your code as you develop your web application before publishing it to the web. The following Power Tip will guide you through setting up a PHP, Apache, and MySQL development environment. Additionally, it will demonstrate how to take advantage of the UEStudio features that will make your PHP/MySQL developement easier.

Create a Local PHP MySQL Development Environment

Note: IDM Computer Solutions, Inc does not provide technical support for Apache, PHP, or MySQL or the installation of these technologies on your system. This Power Tip is intended as an introduction to configuring Apache, PHP, and MySQL on your system in use with the PHP,MySQL, and development functionality in UEStudio.

Step 1. Download the installation files

You will need to download and install the windows version of the installers for each of the below components:

Apache

Apache is the HTTP (Web) server software. http://httpd.apache.org/download.cgi

PHP

PHP is the general-purpose scripting language, especially suited for Web development, that we will be using. http://www.php.net/downloads.php

MySQL Server

MySQL is the database server/software we will be using.

http://dev.mysql.com/downloads/mysql/5.0.html#win32

Step 2: Install Apache

Run the Apache install file. You will need to follow the prompts and respond accordingly. When you reach the window that asks for server information, you may enter the following:

Network Domain: localhost

Server Name: localhost

Administrator Email: (any email address)

Also ensure the "for all Users, on Port 80, as a Service -- Recommended" is checked.

PHP MySQL Development Environment with UEStudio

For this Power Tip, we chose the default options and the default server isntallation path:

c:\Program Files\Apache Group\

Continue clicking "Next" until the installation is complete.

If Apache has been installed correctly, and the server is running, you can type "localhost" in the address bar of your browser. You should see "It Works!" displayed in the browser.

You may also see the apache icon in your system tray:

PHP MySQL Development Environment with UEStudio

When you hover your mouse over the icon, you should see that the services are running. If they are not, you can click on the icon and start the service.

Step 3: Install PHP

Run the PHP installation file which you downloaded in Step 1.

For the purposes of this Power Tip, we have selected the default installation path for PHP, which is:

C:\Program Files\PHP\

Click "Next" until you reach the "Web Server Setup" screen. Here you will need to select the appropriate server. Since we installed the Apache 2.2.xx version of Apache, we have selected "Apache 2.2.x Module":

PHP MySQL Development Environment with UEStudio

The next screen is the "Apache Configuration Directory", here you will need to browse to the location of the Apache configuration directory. Because we installed Apache to the default location, the path we browsed to was:

C:\Program Files\Apache Group\Apache2\conf\

You will be prompted to choose the items you would like to install. PHP does not, by default, install the MySQL database components. You must therefore expand the "Extensions" tree and navigate to MySQL and enable it for installation:

PHP MySQL Development Environment with UEStudio

You will then click next and install. The PHP installer will then complete the installation.

You will be prompted as to whether or not you would like the installer to configure Apache, click on "Yes". You should then see the the Apache configuration file has been successfully updated:

PHP MySQL Development Environment with UEStudio

Click Ok, then click on Finish.

Verify that PHP is running

To ensure that PHP is running on the server, you can create a php file with the following contents:

<?php phpinfo(); ?>

Save the file as "phpinfo.php" in the "htdocs" directory in the Apache installation directory, if you used the default installation path it would be:

C:\Program Files\Apache Software Foundation\Apache2.2\htdocs

Now, in your browser, type:

http://localhost/phpinfo.php

You should see the a PHP information page displayed. You may need to restart the Apache service for PHP to be active. You can restart the service from the Apache icon in the system tray or from the Apache group in the start menu.

If PHP is not running, you may check the Apache error log for further information about the errors. The error logs are located in the "logs" directory in the apache installation directory.

Note: in order to run PHP/MySQL with apache, we had to copy the "php_mysql.dll" to c:\WINDOWS, and the "libmysql.dll" to the c:\WINDOWS\system32. You will find these dlls in the ext directory in the PHP installation directory.

Step 4: Install MySQL

Run the MySQL installation file which you downloaded in Step 1.

Select the option for the "Typical" isntallation

You will then click on "Install".

When the installation is complete, make sure the option for "Configure the MySQL Server Now" is checked (enabled) then click "Finish".

The MySQL Server Instance Configuration Wizard will be launched. Click "Next" until you are prompted for the configuration type. For the purposes of this Power Tip, we have selected the standard configuration:

PHP MySQL Development Environment with UEStudio

Next you will be asked to select the windows options. We have chosen the default options, however we have also enabled the option to "Include Bin Directory in Windows PATH" to allow for command line options.

PHP MySQL Development Environment with UEStudio

Next you will be prompted to create an administrator (root) password:

PHP MySQL Development Environment with UEStudio

Create a password then click "Next". You will then click "Execute" to complete the configuration.

Note: if you are running the Windows Firewall, you may need to temporarily disable it prior to clicking on Execute. If you do not do this the wizard may not be able to finish the configuration. You can disable the firewall in the Windows Control Panel.

Create a Database

To create a database using the command line you will need to open the Command Line Client from the MySQL program group in the windows start menu.

When you open the client, you will be prompted to enter the password for the root account. This is the password that was previously configured. Type the password and hit Enter.

You should see that you have entered the MySQL environment:

PHP MySQL Development Environment with UEStudio

To create a database, type the following command:

CREATE DATABASE uestudio_sample;

where "uestudio_sample" is the name of the database. Note: be sure to include the semi-colon at the end of the command.

If the command is successful you will see something similar to:

Query OK, 1 row affected (0.00 sec)

You may verify that the databases have been created by using the "Show Databases" command. If you type, "Show Databases;" and hit enter, you should see something similar to:

PHP MySQL Development Environment with UEStudio

Create a Table

For the purposes of this Power Tip, we will create a table called "names" that includes the following fields: key, first, last. You may create this table using the following steps:

Type:

USE uestudio_sample;

This will tell MySQL which database to use, then type:

CREATE TABLE names (id INT NOT NULL AUTO_INCREMENT, first VARCHAR(48), last VARCHAR(48), PRIMARY KEY(id));

We will not cover the syntax of the command (above) in the scope of this Power Tip.

If you wish to see the structure of the table, and confirm it was created correctly, type:

Describe Names;

You should see something similar to:

PHP MySQL Development Environment with UEStudio

Now, we need to insert sample data into our table. For example, if we want to create an entry to insert the name "John" (first) "Smith" (last) into the table "names", we would do so using the following command:

INSERT INTO names (first, last) VALUES ('John', 'Smith');

You may insert additional data by modifying the VALUES. Because the id is an "auto increment", we do not need to specify a value for this field.

To display all the data in the table, simply type:

SELECT * FROM names;
Because we inserted a few other names into our table, the query produced the following results:

PHP MySQL Development Environment with UEStudio

Create a User

Now that you have a database, and data, you will want to create a user (account) that will be used by the server (php) to connect to the database. You may access the database with the "root" account, however this is not typically recommended.

To create a user account you will need to use the following command line:

GRANT ALL PRIVILEGES on uestudio_sample.* to idm@'localhost' identified by 'helloworld';
"idm" is the Username and "helloworld" is the password for the user.

Step 5. UEStudio and PHP/MySQL

PHP Support

UEStudio has built in PHP support, which allows you to run scripts, check syntax, and more. If you are interested, you can click here to learn more about the PHP support.

Click on the PHP Commands icon on the script toolbar to configure the PHP support:

PHP MySQL Development Environment with UEStudio

Select the option for "Select PHP Executable", you will then browse to the location of the "php-win.exe" in the PHP installation directory. If you installed to the default path, the php-win.exe would be in

c:\Program Files\PHP\.

After you have selected the executable, you can use the PHP options from the PHP Commands. Simply open a PHP file then click on the options from the PHP Commands icon.

PHP MySQL Development Environment with UEStudio

UEStudio offers additional options that make developing PHP Scripts easier, but before we demonstrate those options we will create a sample PHP script that we can work with.

Programming in PHP is not within the scope of this Power Tip so we will not go into great detail about the script below. This script is simply provided as an example.

The script below will connect to the "uestudio_sample" database running on localhost. This is the database we created earlier. The script will retrieve all the data from the "names" table and output the results in an HTML table.

If you used different values for the variables, you would need to change them for the script to work according to your environment.

<?php //database server define('db_server', 'localhost'); //user, password, and database variables $db_user = 'idm'; $db_password = 'helloworld';     $db_dbname = 'uestudio_sample'; /** * Run MySQL query and output  * results in a HTML Table */ function outputQueryResults() {      //run a select query   $select_query = 'SELECT * FROM names';   $result = mysql_query($select_query);      //output data in a table   echo "<table>\n";   while ($row = mysql_fetch_row($result)){           echo "<tr>\n";       foreach ($row as $val) {           echo "<td>$val</td>\n";       }       echo "</tr>\n";   }   echo '</table>'; } //connect to the database server $db = mysql_connect(db_server, $db_user, $db_password); if (!$db) {    die('Could Not Connect: ' . mysql_error()); } else {   echo "Connected Successfully...\n"; } //select database name mysql_select_db($db_dbname); //run query and output results outputQueryResults(); //close database connection mysql_close($db) ?> 

Create and save the "sample_script.php" in the htdocs directory, which is the same location as the phpinfo.php script from above. If you click on the "Run Script" from the PHP Commands, you should then see the output of the script in the Output window.

PHP MySQL Development Environment with UEStudio

If you would like, you may use the other available commands from the PHP Commands.

To see the contents of the script in the web browser, type http://localhost/sample_script.php in your browser. You should see something similar to:

Connected Successfully...

1JohnSmith
2MikeJones
3MikeJohnson

Create a Project

It is not neccessary to create a project file to develop using PHP/MySQL in UEStudio, however there are added benefits to creating a project. If the files you are editing are part of a project the built-in parser will parse the PHP script giving you a visual representation of the components of the script.

To create a project go to Project->Create New Project. Name the project then click Save. When the Project Settings dialog is presented, click on the Add File button and add the "sample_script.php" to the project.

To view the parsed representation of the file, click on the Files tab in the Workspace manager. If the Workspace manager is not open, you can open it in View->Views/Lists->Workspace Manager. Using the sample_script.php, you should see something similar to the below parsed representation:

PHP MySQL Development Environment with UEStudio

By adding the file to the project, you may also use some of the functions from the IntelliTip Toolbar such as the Find Symbol Definition, Display Functions,

PHP MySQL Development Environment with UEStudio

For a single file, such as this example, there is not much advantage for this. However, if you are using an object oriented style of PHP programming, and your project includes many fies, you may see something that looks more like:

PHP MySQL Development Environment with UEStudio

As you can see, with a large project, this feature can be very helpful.

Link local to remote

If your local development environment is intended to "mirror" your "live" environment, you can link a local folder to a remote folder. This will allow you to sync or upload/download files between the local and remote folder.

No comments On "Install PHP and MySQL on Windows to create a local PHP MySQL dev environment"

Post a Comment

Get Latest Updates

Blog Archive

Total Pageviews