How to Set Up a Local PHP/MySQL Development Environment
The following power tip will guide you through setting up a PHP, Apache, and MySQL development environment on your local Windows system. We’ll also take a look at the UEStudio features that can make your PHP / MySQL development easier.
Please keep in mind that PHP, Apache, and MySQL are software provided by third party entities. Therefore we (IDM) cannot provide technical support for the installation of these on your system.
In days past, you would have had to download Apache server, PHP, and MySQL all separately. However, there are now solutions which include these components bundled together and pre-configured to run “out of the box” on a Windows system. We recommend using WampServer, which provides the basics for setting up your local Apache / PHP / MySQL environment on Windows.
If you do not wish to use an “all-in-one” solution, you will need to download and install the Windows installers for each of the below components:
For the purposes of this power tip, we are assuming that you’ve chosen to install WampServer. To install it, simply double-click the setup file and follow the prompts.
You can confirm that everything has installed properly by pointing your browser to http://localhost/ while Wamp is running. If you see the WampServer “welcome” page, you know everything is installed and running properly. You can also verify this by checking the Wamp icon in the system tray – if the icon is green, then everything is up and running.
Note: You may need to download and install the dependencies described in this community forum post in order for WampServer to run properly on your system.
Since Wamp installs MySQL, all you need to do is set up a user and a database. Click the Wamp icon in the system tray, then go to MySQL » MySQL console. If the console prompts you for a password, just hit Enter.
Create a MySQL database
Now you need to create a new MySQL database. You can do this by issuing the following command in the console:
Make sure to include the semi-colon at the end of the command! The console should give a “query ok” response.
You can 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:
Make sure to include the semi-colon at the end of the command! The console should give a “query ok” response.
You can 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:
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. Create this table in our “uestudio_test” database by using the following commands:
…then:
We won’t cover the syntax of these commands in the scope of this power tip.
You can see the structure of the table and confirm it was created correctly by typing:
You should see something similar to:
Now, we need to insert some 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:
You can insert additional data by modifying the VALUES. Because you created the column id as an auto increment, you don’t need to specify a value for this field.
To display all the data in the table, simply type:
Because we inserted a few other names into our table, this query produced the following results:
Create a user
It’s never a good idea to use the “root” user in MySQL to interact with your databases. Create a new user and assign it to your database in the MySQL console by issuing the following command:
Obviously you will want to modify the user name and password in the above to suit your preferences. You should see the console spit out:
PHP Support
UEStudio has built in PHP support which allows you to run scripts, check syntax, and more. To configure PHP support, click the Coding tab then open the PHP drop down. Click the last item here to set the location of the PHP executable.
Click the Browse button to browse to and select the folder containing the PHP executable, which in a default Wamp installation would be:
C:\wamp64\bin\php\php7.1.9
Of course this folder path may vary based upon what version of PHP you have installed and running.
After you’ve set the PHP executable’s parent folder, you can use the PHP options in the Coding tab. Simply open a PHP file then click on any of the options from the PHP icon.
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.
Learning to code PHP is not within the scope of this power tip, so we won’t go into great detail about the script below. This script is simply provided as an example.
The script below will connect to the “uestudio_test” database running on localhost. This is the database we created earlier. The script connects, then retrieves 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 // Connect to database server and database $mysqli = new mysqli('localhost', 'SomeUser', 'somepassword', 'uestudio_test'); // If connection attempt failed, let us know if ($mysqli->connect_errno) { echo "Sorry, this website is experiencing problems."; echo "Error: Failed to make a MySQL connection, here is why: \n"; echo "Errno: " . $mysqli->connect_errno . "\n"; echo "Error: " . $mysqli->connect_error . "\n"; exit; } function outputQueryResults($mysqli) { $sql = 'SELECT * FROM names'; // run the query if (!$result = $mysqli->query($sql)) { // Handle error echo "Sorry, this website is experiencing problems."; echo "Error: Query failed to execute, here is why: \n"; echo "Query: " . $sql . "\n"; echo "Errno: " . $mysqli->errno . "\n"; echo "Error: " . $mysqli->error . "\n"; exit; } // If zero rows.... if ($result->num_rows === 0) { echo "This query resulted in no matches returned. Please try again."; exit; } //output data in HTML table echo "<table>\n"; while ($row = $result->fetch_assoc()) { echo " <tr>\n"; echo " <td>" . $row['first'] . "</td>\n"; echo " <td>" . $row['last'] . "</td>\n"; echo " </tr>\n"; } echo "</table>"; } // run query and output results outputQueryResults($mysqli); // close database connection mysqli_close($mysqli); ?>
You can try the other commands available in the PHP drop down.
To see the contents of the script in the web browser, type http://localhost/mysql_test.php in your browser. You should see something similar to:
1 | John | Smith |
2 | Bob | White |
3 | Mike | Jones |
Create a Project
You don’t have to create a project to develop using PHP/MySQL in UEStudio, but there are added benefits to creating a project. If the files you are editing are part of a project, then UEStudio’s IntelliTips will parse the PHP script, which provides you a visual representation of classes, functions, etc. in the Classviewer, and provides some context-aware auto-complete when typing source code.
To create a project, click on New project in the Project tab. Name and save the project file. The “Project settings” dialog will open, and you can add the “mysql_test.php” to the project by clicking + Active file.
To view the parsed representation of the file, click on the Tags tab at the bottom of the Workspace manager. If the Workspace manager is not open, you can open it by checking Workspace manager in the Layout tab. Using the mysql_test.php file you created in this tutorial, you should see something similar to the below parsed representation:
By adding your source files to a project, you can also access IntelliTips functionality in the Project tab, like Find symbol definition, Display functions, etc.
By adding your source files to a project, you can also access IntelliTips functionality in the Project tab, like Find symbol definition, Display functions, etc.
For a single file like in this example, there really isn’t much advantage for this. However, if you are using an object-oriented style of PHP programming, and your project includes many files, you would see something that looks more like:
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 site, 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 in a single click. Learn more about linking local folders to remote folders.