DEV Community

Cover image for How to run PHP and link to MySQL using XAMPP
Aakriti Sharma
Aakriti Sharma

Posted on

7

How to run PHP and link to MySQL using XAMPP

I spent 12 hours on the day of my presentation trying to figure this out so if you ask me what was the inspiration behind writing this post, it's to save someone from this plight.

Installing PHP:

Go to https://www.php.net/downloads.php
I downloaded 7.2, on clicking on the link a zip folder gets downloaded, extract the files inside a folder PHP7 in the C drive.

If you use VSCode, change php.validate.executablePath = "C://PHP7//php.exe" in settings.json
Install the Code Runner Extension and run the file from terminal itself.

Installing XAMPP

Go to https://www.apachefriends.org/download.html
and download the latest version of XAMPP, installation process begins, press Finish when it is done.

Open XAMPP Control Panel
Press Start for Apache and MySQL modules.
The panel appears like: Control Panel

Go to your browser and type localhost
If the following screen appears , you have installed XAMPP successfully Dashboard

Run PHP Files using XAMPP

Go to C Drive -> xampp -> htdocs -> create a folder and store your php files there.

Inside your browser go to localhost/your folder name/path to your php file and you will be able to see your output on the webpage.

Linking PHP to Database

$host = "127.0.0.1";
$username = "root";
$pass = "foobar";
$conn = mysqli_connect($host, $username, $pass, "speed_age");
if(!$conn)
{
echo 'could not connect'.mysqli_error();
}

The next error I encountered was root@localhost not connected using password if you do too go to XAMPP Control Panel -> Config button for Apache Module -> phpMyAdmin -> Section under Authentication type and info

Change this line as $cfg['Servers'][$i]['password'] = 'your password';

I have used foobar so I changed it to $cfg['Servers'][$i]['password'] = 'foobar';

If you get the no database detected error go to Admin in MySQL Module -> Databases -> Create Database -> Give the name as you used in your code , I used speed_age in mine.

For creating tables and manipulate data in rows use GUI or go to SQL tab right to Databases and run query.

Thank you for reading , this is my first post let me know what you think :)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (2)

Collapse
 
yellow1912 profile image
yellow1912 •

I used xammp before, let me tell you I trick that will save you ton of time and headache in the future. Stop using it. Learn how to setup php, mysql, apache yourself. It's well worth the time. You can use wsl to create linux environment on your system, then learn how to properly configure your php environment. It will save you many hours in the future trying to wrestle with xammp. Plus when you actually deploy your code on a real server, you already know your way around.

Collapse
 
aakriti_sharma profile image
Aakriti Sharma •

Thank you for the heads up!

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay