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

Konrad Pilch
Konrad Pilch
2,435 Points

How to connect to the database

HI,

How can i make a OOP database class? and why and what should be in there?

Isnt just enoguh to put the localhost password, name etc.. ?

And what should i put when i include files? should i put include or request _ once? whats the difference? also, if the user is logged in , and i want to show something else, i put it in the include , so if the user is logged in, include this part of a template which is his name etc..

PLus, how would i create a SQL class ? meaning if i want to select all the SELECT * FROM users and put it in function, so when the user register, i call this method or class and it does the wokr for me ? how does this wokr?

Iv been throw Hampton tutorial, but now im trying to use it in real life. The tutorial could be extended a bit more into a real life with some db :(

3 Answers

Scott Evans
Scott Evans
4,236 Points

Have a look at this Github Repo. It contains an example of a database class me and an old friend created on used. It encapsulates query management Query security and a few other useful tools in Small - Medium projects.

https://github.com/aJamDonut/bSimple-PHP-MySQL-DB-Class/blob/master/mysqli.database.inc.php

Konrad Pilch
Konrad Pilch
2,435 Points

So all that code will go to the database?

And would i require _ once the database as well when including to the toher page?

ANd thank you have a read about that :)

Scott Evans
Scott Evans
4,236 Points

With the class i am referring to, you would

<?php
require("mysqli.database.inc.php");
$db = new Database($user='root', $pass='123', $host="localhost");

$db->_one("select name from users where username=%s","Adam");
?>