DEV Community

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

Posted on

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 :)

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!