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! While you're at it, check out some resources Treehouse students have shared here.

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

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

how in workspaces can I connect to a a database?

My friend has sent me a project to look through. However I could not get it to load in my WAMP for some reason.

So I have decided to put it in workspaces. My question here is how do I create or link the database to mysql. Comment in my code below can be seen where I need to change some code;

<?php
namespace Tutor\Provider;

use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\Session\Session;

class Tutor {

    private $link;

    public function __construct() {
        $this->$link = new mysqli_connect("localhost","mayur","","londontutors"); //here what should I change this to?
    }

    public function get_tutor($username) {
        $username = mysqli_real_escape_string($this->link, $username);
        $result = mysqli_query($this->link, "select tutor.email, name, username, subjects, summary, about, image from tutor inner join user on tutor.email = user.email where username = '$username'");
        if ($tutor = mysqli_fetch_assoc($result)) {
            $result = mysqli_query($this->link, "select subject from tutorexperience where email = '{$tutor['email']}'");
            $tutor['experience'] = array();
            while ($row = mysqli_fetch_assoc($result)) {
                $tutor['experience'][] = $row['subject'];
            }
            $result = mysqli_query($this->link, "select qualification from tutorqualification where email = '{$tutor['email']}'");
            $tutor['qualifications'] = array();
            while ($row = mysqli_fetch_assoc($result)) {
                $tutor['qualifications'][] = $row['qualification'];
            }
            return $tutor;
        } else {
            return null;
        }
    }

    public function get_tutors() {
        $result = mysqli_query($this->link, "select tutor.email, name, username, subjects, summary, about, image from tutor inner join user on tutor.email = user.email");
        $tutors = array();
        while ($tutor = mysqli_fetch_assoc($result)) {
            $tutors[] = $tutor;
        }
        return $tutors;
    }

    public function get_group_tuition($email) {
        $email = $email || '';
        $email = mysqli_real_escape_string($this->link, $email);
        $result = mysqli_query($this->link, "select tutoremail, starttime, endtime, location, class, level, topic, capacity, (capacity - (select count(*) from grouptuitionbooking where grouptuitionbooking.tutoremail = grouptuition.tutoremail and grouptuitionbooking.starttime = grouptuition.starttime)) as available, username, name, (select '$email' in (select studentemail from grouptuitionbooking where grouptuitionbooking.tutoremail = grouptuition.tutoremail and grouptuitionbooking.starttime = grouptuition.starttime)) as booked from grouptuition inner join (select tutor.email, tutor.username, user.name from tutor inner join user on tutor.email = user.email) t on grouptuition.tutoremail = t.email and starttime >= NOW() order by starttime asc, username asc");
        $groups = array();
        while ($group = mysqli_fetch_assoc($result)) {
            $group['datetime'] = date("l jS g", strtotime($group['starttime'])) . '-' . date("ga", strtotime($group['endtime']));
            $groups[] = $group;
        }
        return $groups;
    }

    public function get_group_tuition_details($tutoremail, $starttime) {
        $tutoremail = mysqli_real_escape_string($this->link, $tutoremail);
        $starttime = mysqli_real_escape_string($this->link, $starttime);
        $result = mysqli_query($this->link, "select tutoremail, starttime, endtime, location, class, level, topic, capacity, (capacity - (select count(*) from grouptuitionbooking where grouptuitionbooking.tutoremail = grouptuition.tutoremail and grouptuitionbooking.starttime = grouptuition.starttime)) as available, username, name from grouptuition inner join (select tutor.email, tutor.username, user.name from tutor inner join user on tutor.email = user.email) t on grouptuition.tutoremail = t.email and grouptuition.tutoremail = '{$tutoremail}' and grouptuition.starttime = '{$starttime}' order by starttime asc, username asc");
        if ($group = mysqli_fetch_assoc($result)) {
            $group['datetime'] = date("l jS g", strtotime($group['starttime'])) . '-' . date("ga", strtotime($group['endtime']));
            $result = mysqli_query($this->link, "select email, name, address, phone from grouptuitionbooking inner join user on studentemail = email and tutoremail = '{$tutoremail}' and starttime = '{$starttime}'");
            $group['students'] = array();
            while ($student = mysqli_fetch_assoc($result)) {
                $group['students'][] = $student;
            }
            return $group;
        } else {
            return null;
        }
    }
}

class TutorServiceProvider implements ServiceProviderInterface {

    public function register(Application $app) {
        $app['tutor'] = function () {
            return new Tutor();
        };
    }

    public function boot(Application $app) {
    }
}
?>

1 Answer

Jeremy Canela
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jeremy Canela
Full Stack JavaScript Techdegree Graduate 30,766 Points

It is unfortunately not possible to connect to databases in Workspaces. Try installing XAMPP "the most popular PHP development environment". I love it because it's easy to setup.

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

thank you for the answer.

Do you know anything about changing the line I want to change (below) in XAMPP?

$this->$link = new mysqli_connect("localhost","mayur","","londontutors"); //here what should I change this to?
Jeremy Canela
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jeremy Canela
Full Stack JavaScript Techdegree Graduate 30,766 Points

Remove the $ from $link in $this->$link. It should be $this->link. Let me know if it works. Plus, you only use the new keyword when you call a class. mysqli_connect is a function.