Writing PHP with MySQL in Linux
Configuration Instructions
- Download Visual Studio Code in
your virtual machine (VM). Be sure to use the
.deb
package. - Install Visual Studio Code.
- Open Visual Studio Code and clone your project repository. The URL to clone your repository can be retrieved using the green “Code” button in GitHub.
-
Create a file with the SQL commands to create your database (i.e., all the tables) and to populate the database with data. (These commands were part of the schema design.)
This file will be used to ensure that the database exists and contains representative data, such as that shown in your project demonstration. The file should have a
.sql
extension and be saved in your local repository. See the REST API exercises for an example.Use the following template, which drops and (re)creates the database each time it is executed:
-- drop the database (if it exists) DROP DATABASE IF EXISTS CollaborativeDigitalToolkit; -- create the database CREATE DATABASE CollaborativeDigitalToolkit; -- grant privileges to the student user GRANT ALL PRIVILEGES ON CollaborativeDigitalToolkit.* TO 'student'@'localhost'; -- use the newly-created database for subsequent queries USE CollaborativeDigitalToolkit; -- -- TODO: Update remainder with relevant queries -- -- create tables -- insert data
where “CollaborativeDigitalToolkit” is a placeholder for the name of your project.
- Open a terminal in the VM.
-
Change the working directory to your local repository.
For example, assuming that your local repository is in
~/public_html
, use the following command to change to that directory:$ cd ~/public_html/collaborative-digital-toolkit
-
Run the SQL file created previously:
$ sudo mysql < CollaborativeDigitalToolkit.sql
A lack of any error message(s) indicates that the SQL statements were successful.
Now your PHP files should be able to connect to the database. For example,
<?php
$connection = new mysqli("localhost", "student", "CompSci364",
"CollaborativeDigitalToolkit")
or die("Connection failed: ".mysqli_connect_error());