Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Integrating PHP with Databases Databases and PHP Getting Started with PDO

This lesson in Visual Studio Code

Is it possible to do this lesson in Visual Studio Code? If so, how?

3 Answers

After some research I found a solution that worked for me.

  1. Download XAMPP
  2. Start mySQL in XAMPP, it should be on port 3306
  3. In Visual Studio Code download and enable the extension "PHP Server" by baprifa (Ignore this step if you already have, or if are using a different php server. This is the one I happen to be using)
  4. In Visual Studio Code download and enable the extension "MySQL - MySQL management tool" by Jun Han
  5. Reload Visual Studio Code to ensure it's activated.
  6. In the Visual Studio Code Explorer you will now notice a MYSQL drop down tab at the very bottom. There should be a localhost database in there. If it's not appearing or shows an error, open XAMPP again to make sure you started mySQL
  7. You should now be up and running. To upload the project to phpMyAdmin in XAMPP, open the database.sql file that was downloaded from treehouse for the project. Right click anywhere on the document, and select option that says "Run MySQL Query".
  8. You can verify you are connected by opening the XAMPP control panel, Start the Apache Server, Open Web Browser and type localhost
  9. It will bring you to XAMPP page hosted locally on your computer. From there click phpMyAdmin link. On the left hand side verify that your database was uploaded.

NOTE: in this exercise the teacher is using the SQLite (PDO) driver. Instead, you will need to use the MySQL (PDO) driver.

To test that everything is working, in the inc folder, create a connection.php file. This is the code I used to test:

<?php

$db = 'mysql:host=localhost;port=3306;dbname=database';
$username = 'root';
$password = '';

$dbh = new PDO($db, $username, $password);

$query = "SELECT * FROM people";

$result = $dbh->query($query);

foreach($result as $item) {
    echo "[" . $item['people_id'] . "] " . $item['fullname'] . "<BR>";
}

After saving it, right click and choose PHP Server: Serve Project to view.

NOTE: If both the Apache Server in XAMPP, and PHP Server by baprifa in VS Code are running, the PHP Server will be the one that works. To use phpMyAdmin you must stop the PHP Server.... at least that's how it is on my system.

I hope this info is helpful. I am new to this, so if there is anyone with more experience who would like to add or correct anything I may have typed, please feel free to comment.

theodevries
theodevries
15,895 Points

Hi jaycode,

I don't see why not. But you need a local development server. Do you have that set up? If yes then download the files below the video under the "download" tab. Use the sql file to create a database with.

If you open the downloaded files in Visual Studio Code you would be able to follow along.

Succes, Kind Regards

I've been using the VSC extension "PHP Server" by baprifa. It works fine for opening php files, but I'm totally lost when it comes to connecting it to MySQL. In the past I've been using Xampp & phpMyAdmin, but it would be nice if I can consolidate everything thru Visual Studio Code.

I have no idea where to begin. If anyone can point me in the right direction it would be greatly appreciated. Thanks!