phpinfoFile
Get a Free Server Check

Connect PHP to MySQL in XAMPP (Local Setup)

How XAMPP supports local PHP development

XAMPP gives you a local server setup for building and testing PHP websites. It bundles Apache, PHP, MySQL, and phpMyAdmin. Apache serves your pages, while MySQL stores their data.

phpMyAdmin is a browser tool for managing MySQL. It is not the database itself. Your PHP code connects to the MySQL server, not to the phpMyAdmin page.

If you are learning how to connect to database in PHP using XAMPP, start with these parts. Apache and MySQL must both run before your page can use the database. For a basic local setup, the server name is usually localhost, the user is often root, and the password may be blank.

Install XAMPP and start its servers

Get the installer from the Apache Friends XAMPP site. Keep Apache, PHP, MySQL, and phpMyAdmin selected. The default install folder is fine for most local projects.

Open the XAMPP Control Panel and start Apache and MySQL. Then visit http://localhost/ in your browser. If the XAMPP page loads, Apache is running.

Visit http://localhost/phpmyadmin/ to open the database tool. If MySQL will not start, another app may be using its port. Check the MySQL log before changing settings.

Save your PHP files in XAMPP's htdocs folder. For example, a file at htdocs/shop/test.php opens at http://localhost/shop/test.php. This keeps your test page within Apache's local web folder.

Local computer workspace prepared for running Apache and MySQL with XAMPP
Starting a local XAMPP server

Connect PHP to MySQL with MySQLi or PDO

To connect the database in PHP, you need four settings: host, username, password, and database name. XAMPP often uses localhost and root. Use the password set on your own MySQL server.

MySQLi works with MySQL databases. PDO supports several database types, so it can help if a project may switch later. Both offer prepared statements for safe data handling.

Here is a MySQLi connection for a database called shop_local. Put it in a PHP file under htdocs. The code stops if the connection fails.

MySQLi connection: $conn = new mysqli('localhost', 'root', '', 'shop_local');

Check for an error: if ($conn->connect_error) { die('Connection failed'); }

PDO uses a data source name to set the host, database, and character set. A try and catch block handles a failed connection without showing private server details.

PDO connection: try { $pdo = new PDO('mysql:host=localhost;dbname=shop_local;charset=utf8mb4', 'root', ''); }

Handle failure: catch (PDOException $e) { die('Database connection failed'); }

These examples show how to connect SQL with PHP on a local MySQL setup. They do not connect PHP to the phpMyAdmin interface. PHP talks to MySQL directly, using the same database settings you use in phpMyAdmin.

On a live website, do not use the root account. Make a separate database user with only the access your app needs. The PHP manual's PDO connection notes explain the connection string and options.

Laptop and database table sketch representing a PHP connection to MySQL
PHP connection to a MySQL database

Create a database and table

In phpMyAdmin, choose New, enter shop_local, and click Create. Pick the utf8mb4 character set if the page offers that choice. It supports a wide range of text and symbols.

You can also make the database with an SQL command. Open phpMyAdmin's SQL tab and run this:

Create database: CREATE DATABASE shop_local CHARACTER SET utf8mb4;

Select the new database, then create a table named contacts. This table stores a numeric ID and a name. The ID rises by one for each new row.

Create table: CREATE TABLE contacts (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);

The same table can be made in phpMyAdmin's Structure view. Set id as the primary key and turn on auto increment. Choose a field size that fits the data you plan to save.

Insert and retrieve data with PHP

Use a prepared statement when a value comes from a form. It keeps the value apart from the SQL command. That helps stop user input from changing the query.

This MySQLi example adds a name to the contacts table. A real HTML form should check that the field is present and valid before saving it.

Insert a row: $name = 'Sam Lee';

Prepare and run: $stmt = $conn->prepare('INSERT INTO contacts (name) VALUES (?)'); $stmt->bind_param('s', $name); $stmt->execute();

For a form, read the submitted value from $_POST, then bind it in the same way. Set the form method to post and give its name field a matching name attribute. This is the basic path for connecting an HTML form to a MySQL database using PHP.

To read saved rows, run a SELECT query and loop through the results. Escape any value before showing it in a web page. This prevents saved text from being treated as page code.

Read rows: $result = $conn->query('SELECT id, name FROM contacts');

Show names safely: while ($row = $result->fetch_assoc()) { echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8'); }

For most small projects, store image files in a folder and save their file paths in MySQL. Storing full image data in the database can make backups and page loads harder to manage. Limit uploads by file type and size.

Check the connection and fix common errors

Make a small test page that opens the connection and prints a short success message. Load it through localhost, not by opening the PHP file directly. If the page says the connection failed, check that MySQL is running first.

Next, compare the host, user, password, and database name in your PHP code. A misspelled database name is a common cause. Also confirm that the database exists in phpMyAdmin.

If Apache cannot load the page, check the file path and the URL. If MySQL reports a port issue, another service may be using its usual port. The XAMPP logs can help narrow down the cause.

Once the test works, try inserting one row and viewing it in phpMyAdmin. Then load it with a SELECT query. This confirms that PHP can both write to and read from the database.

Need it done for you? Contact us about your server.

← All PHP server notes