Search

An Introduction to PHP and MySQL

5 min read
0 views

Getting Started: Installing Apache on Windows

Apache is the most widely used web server, and the Windows port is a dependable choice for developers who want to build an e‑commerce or database‑backed site locally. The first thing to do is grab the installer. The official Apache Lounge site offers the most recent binary builds for Windows:

Prompt
<!DOCTYPE html></p> <p><html lang="en"></p> <p><head><title>Apache Test</title></head></p> <p><body></p> <p> <p>Apache is running successfully!</p></p> <p></body></p> <p></html></p>

Refresh http://localhost/test.html in your browser and verify the message displays. This confirms that file serving is operational and that your document root is correctly configured. With Apache up and running, we can now introduce PHP into the mix.

Bringing PHP into the Mix: Setting Up PHP with Apache

PHP is a server‑side scripting language that works hand‑in‑hand with Apache. The easiest route for most developers is to use the pre‑compiled Windows binaries that include the CGI or FastCGI executable. Head over to the PHP downloads page:

Prompt
LoadModule php_module "C:/php/php7apache2_4.dll"</p> <p>AddHandler application/x-httpd-php .php</p> <p>PHPIniDir "C:/php"</p>

These directives load the PHP module for Apache 2.4, specify that files with a .php extension should be processed by PHP, and point PHP to its configuration file. If you’re using a newer PHP version (8.x), replace the DLL name accordingly – the file will have a similar name with the PHP version number embedded.

The next step is to configure PHP itself. Inside the C:\php folder, find php.ini-production and copy it to php.ini. Open php.ini in a text editor and search for the line that begins with extension_dir. Ensure it points to C:/php/ext. This is where PHP’s optional extensions live. If you want to enable extensions such as mysqli or pdo_mysql, locate the corresponding extension=php_mysqli.dll line, remove the leading semicolon, and save the file. For a minimal installation, the default extensions are usually enough.

To test that PHP is correctly integrated, create a new file in the document root called phpinfo.php with the following single line:

Prompt
<?php phpinfo(); ?></p>

Open your browser and navigate to http://localhost/phpinfo.php. A long page of configuration information should appear. Look for a section titled Server API – it should read Apache 2.4 Handler. Verify that the PHP version matches the one you installed and that the Loaded Configuration File path points to C:/php/php.ini. If everything looks correct, you can close the test file; it’s a handy reference for debugging in the future.

Now that PHP is talking to Apache, you’re ready to add a database layer. The next section covers installing MySQL, creating a database, and writing PHP code that interacts with it.

Building a Database with MySQL and Connecting it to PHP

MySQL is the go‑to relational database for many web projects. Download the latest Windows installer from the official site: https://dev.mysql.com/downloads/mysql/. Choose the ZIP archive for “Windows (x86, 32‑bit) or 64‑bit” and extract it to C:\mysql. The archive contains the server binaries, command‑line tools, and the MySQL Workbench GUI, which is optional but helpful for visual database design.

Run the installer by double‑clicking setup.exe inside the extracted folder. The wizard guides you through setting up the MySQL Server. For a local development environment, the default options work fine: select Server as the product type, choose a root password (you’ll need it later), and finish the installation. Once the service starts, you can verify it by opening a command prompt and typing mysql -u root -p. Enter the password you chose; you should see the MySQL prompt mysql>

With the server running, the next step is to create a database that PHP can use. There are two common approaches: use the MySQL command line or use PHP itself. The command‑line method is straightforward and quick.

From the MySQL prompt, execute:

Prompt
CREATE DATABASE ecommerce;</p> <p>USE ecommerce;</p>

This creates a database named ecommerce and switches to it. You can now create tables, insert data, and run queries. For example, to build a simple products table, run:

Prompt
CREATE TABLE products (</p> <p> id INT AUTO_INCREMENT PRIMARY KEY,</p> <p> name VARCHAR(255) NOT NULL,</p> <p> price DECIMAL(10,2) NOT NULL,</p> <p> stock INT DEFAULT 0</p> <p>);</p>

Insert a few sample rows:

Prompt
INSERT INTO products (name, price, stock) VALUES</p> <p>('Wireless Mouse', 19.99, 150),</p> <p>('Keyboard', 49.99, 80),</p> <p>('USB‑C Cable', 9.99, 200);</p>

Alternatively, you can create the database directly from PHP. Open a new file named create_db.php in the document root and add the following code:

Prompt
<?php</p> <p>$mysqli = new mysqli('localhost', 'root', 'your_root_password');</p> <p>if ($mysqli->connect_error) {</p> <p> die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);</p> <p>}</p> <p>$mysqli->query("CREATE DATABASE IF NOT EXISTS ecommerce");</p> <p>$mysqli->query("USE ecommerce");</p> <p>$mysqli->query("CREATE TABLE IF NOT EXISTS products (</p> <p>)");</p> <p>echo 'Database and table created.';</p> <p>$mysqli->close();</p> <p>?></p>

Replace your_root_password with the actual root password you set earlier. Visiting http://localhost/create_db.php will run the script and output “Database and table created.” Either method yields the same result; choose the one you feel more comfortable with.

At this point you have a MySQL database populated with data and PHP configured to talk to it. The next step is to build a simple PHP script that reads from the database and displays the contents to the user.

Testing the Stack: A Simple CRUD Example

Now that the three core components - Apache, PHP, and MySQL - are working together, it’s useful to confirm that PHP can query the database and render results in a browser. Create a file named products.php in the document root with the following content:

Prompt
<?php</p> <p>$mysqli = new mysqli('localhost', 'root', 'your_root_password', 'ecommerce');</p> <p> die('Connection failed: ' . $mysqli->connect_error);</p> <p>}</p> <p>$result = $mysqli->query('SELECT id, name, price, stock FROM products');</p> <p>?> <!DOCTYPE html></p> <p><head></p> <p> <meta charset="UTF-8"></p> <p> <title>Product List</title></p> <p></head></p> <p><body></p> <p> <h1>Available Products</h1></p> <p> <table border="1" cellpadding="5" cellspacing="0"></p> <p> <tr>&ltth>ID</th><th>Name</th><th>Price</th><th>Stock</th></tr></p> <p> <?php</p> <p> while ($row = $result->fetch_assoc()) {</p> <p> echo '<tr>';</p> <p> echo '<td>' . $row['id'] . '</td>';</p> <p> echo '<td>' . htmlspecialchars($row['name']) . '</td>';</p> <p> echo '<td>$' . number_format($row['price'], 2) . '</td>';</p> <p> echo '<td>' . $row['stock'] . '</td>';</p> <p> echo '</tr>';</p> <p> }</p> <p> ?></p> <p> </table></p> <p></body></p> <p></html></p> <p><?php</p> <p>$mysqli->close();</p> <p>?></p>

Replace your_root_password with the actual password. Opening http://localhost/products.php should present a simple table of the three sample products. The script demonstrates a full read operation (SELECT) from MySQL, handling the result set, and safely escaping output with htmlspecialchars to avoid cross‑site scripting risks.

To extend the example, you could add a form that submits a new product to the database. The POST data would be captured in PHP, sanitized, and inserted with an INSERT query. A second script could delete or update existing rows using UPDATE or DELETE. Even a basic CRUD interface becomes a solid foundation for an e‑commerce site.

With Apache serving the pages, PHP handling logic, and MySQL storing data, you now have a complete LAMP‑like stack running on Windows. The learning curve is modest, and the result is a robust environment that supports a wide range of web applications. Explore additional PHP libraries, secure your MySQL connections, and consider deploying to a production server when you’re ready to take the next step.

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!

Related Articles