RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Developing PHP Applications with NetBeans and Eclipse : Page 4

Discover the convenience of developing PHP applications using two of the most popular and powerful IDEs on the market, NetBeans and Eclipse.


Implementing booksPHP Code in Eclipse

As you know, the booksPHP project contains two additional PHP pages, named database.php and search.php. To create a PHP page in Eclipse follow next steps:

  1. In the Project Explorer view, right-click on the project name and select New → Other from the context menu.
  2. In the New window, expand the PHP node and select the "PHP File" leaf item. Click Next.
  3. On the next page, set the Source Folder to your project location, and enter database.php (or search.php) in the File Name field. Click Next.
  4. Finally, select a template for the initial content. In this case, select the "New simple PHP file" and option, and click Finish.
While implementing PHP code don't forget to take advantages of Eclipse's PHP views and the code assistant.

Implementing AJAXjs.js—Eclipse Style

To complete the project code, you need to implement the JavaScript AJAX portion. To create a JavaScript file in Eclipse:

Figure 13. Eclipse Code Entry Features: You can see some code highlighting and code assistance features in action in this figure.
  1. In the Project Explorer view, right-click on the project name and select New → Other from the context menu.
  2. In the New window, expand the JavaScript node and select the JavaScript Source File item. Click Next.
  3. On the next page, specify the page name in the File name field (AJAXjs.js in this case), and click Finish.
Eclipse adds the new JavaScript file and opens it in the editor. Before entering the code, you should know that Eclipse offers a JavaScript perspective and a set of dedicated views that you can activate from the Window → Open Perspective → Other → JavaScript menu.

Author's Note: Eclipse also provides a JavaScript validator (reachable through the Window → Preferences → JavaScript → Validator menu); however, I don't recommend using it right now, because the validator has problems understanding JavaScript code.

While typing the AJAXjs.js code, be sure to take advantage of Eclipse's code highlighting and code assistance features (see Figure 13).

Create the Books Database—Eclipse Style

Figure 14. Installing the Database Development Perspective: In the Available Software panel, find the Ganymede Update Site, check it, and it will populate a list of plug-ins, among which you'll find Database Development. Check that and click Install to download and install the plug-in.
The Eclipse PDT doesn't offer support for database development. But Eclipse has many plug-ins, among which is the Eclipse Data Tools Platform. According to the Eclipse site, this feature "provides extensible frameworks and exemplary tools, enabling a diverse set of plug-in offerings specific to particular data-centric technologies and supported by the DTP ecosystem." For the purposes of this project, you should know that DTP adds (among other things), a new perspective, named Database Development, that will help you develop the books database. Installing this feature is a simple task that you can accomplish as follows:

  1. From the Help menu, select Software Updates.
  2. Switch to the Available Software tab.
  3. In the Available Software panel, locate the Ganymede Update Site entry (if it is not in this list, then click the Manage Sites button and activate it from that list).
  4. Check the Ganymede Update Sites item and wait for it to populate the list (you should see something similar to Figure 14).
  5. Select the Database Development checkbox and click the Install button.
  6. You'll see a dialog similar to Figure 15.

    Figure 15. Database Development Items: Check the items that you want to install.

  8. Select all the checkboxes and click Next. From this point, the install process is clear and intuitive, so I won't go into more detail here. Just as a reminder, though, when the installation completes, restart Eclipse before you continue.
If you get here, then you probably successfully installed DTP, and you're ready to develop the books database. To begin, launch the Database Development perspective by clicking the Window → Open Perspective → Other → Database Development menu item.

Author's Note: Eclipse needs a special driver to connect to a RDBMS such as MySQL, PostgreSQL, etc. You can download the MySQL Connector/J driver for this purpose (MySQL Connector/J is the official JDBC driver for MySQL). After unzipping the driver, you should obtain a Java Archive named mysql-connector-java-5.1.7-bin.This archive should be located in a convenient place on your machine (in the MySQL home folder can be a solution).

Now, in the left side of the screen you should see the Data Source Explorer view. Because you just installed DTP, you don't have any database connections yet, so here's how to create a connection to MySQL:

  1. Start your MySQL server.
    Figure 16. Define New Database Driver: Click the New Driver Definition button to define and configure a driver for MySQL.
  3. In the Data Source Explorer view, right-click on Database Connections and select New from the context menu.
  4. In the New Connection Profile wizard, select MySQL from the list of connection profile types, and click Next. You may optionally type a name and a description for this profile.
  5. Next select a driver for MySQL. Because you won't find one defined by default, configure it like this:
  6. Click the New Driver Definition icon (see Figure 16).
  7. In the New Driver Definition window, on the Name/Type tab, select "MySQL JDBC Driver for 5.0 system version." (If you don't see the driver templates, expand the Database node.)
    Figure 17. Driver Details: Enter the MySQL driver details as shown here.
  9. Switch to the Jar List tab and remove all the driver files from the corresponding list (use the Clear All button).
  10. Click Add JAR/Zip button and point to the MySQL Connector/J driver you downloaded earlier.
  11. Switch to the Properties tab and set the properties as shown in Figure 17:
  12. Click OK.
  13. Click Finish.
In a few moments, if no errors occurred, you should see something similar to Figure 18 in the Data Source Explorer:

Figure 18. New Database Connection: After adding the MySQL driver and configuring the connection, the new connection appears under the Database Connections node.

Figure 19. Creating and Populating the Books Database: Here's the SQL to create and populate the books database in the Eclipse Open SQL Scrapbook.
Once again, you need to execute some SQL to create the books database, the bookstore table, and populate that table. To do that, right-click on the MySQL_books connection and select the Open SQL Scrapbook item. That launches an SQL editor where you can enter/edit the desired SQL statements (see Figure 19).

Save this file as books.sql under your project root.

Finally, execute the SQL statements. To do that, switch back to the PHP perspective. In the PHP Explorer view, expand the booksPHP node and right-click on the books.sql file. Select Execute SQL files from the context menu and wait until the database is created. The execution status gets reported in SQL Results view, which you can activate by clicking Window → Show View → Other → Data Management.

Author's Note: To connect or disconnect from a database, select the Connect/Disconnect option from the context menu that appears when you right-click on a database connection in the Data Source Explorer view in the Database Development perspective.

Run booksPHP—Eclipse Style

Running the application is simple. First, ensure that Apache HTTP Server and MySQL Server are running properly. Next, select index.php in PHP Explorer view and select Run → Run As → PHP Web Page from the menu. You will be prompted for the URL (which for this example is http://localhost/php/booksPHP/index.php), and you should see the application open in a browser.

Author's Note: From the Run → Run As menu you can also elect to run PHP scripts using the PHP interpreter.

In closing, it's important to mention two things: First, this article is not a NetBeans vs. Eclipse for PHP comparison piece. Instead, I've chosen to simply provide walkthroughs, and leave it to developers to choose which IDE is best for them. Second, I've intentionally skipped debugging, because the topic is too large, and trying to include it here wouldn't do it justice.

Finally, because I just told you what this article is not, let me revisit what it is! The goal of this article was to present some of the main PHP features in NetBeans and Eclipse. You saw how to capitalize on some of the most convenient features, such as creating new projects, using auto-completion, connecting to MySQL, creating and modifying databases directly from the IDE, and deploying and running PHP applications in easy and elegant ways. Whichever IDE you choose, you'll find that it will improve your development.

Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date