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

Whats the difference? connecting to database

Whats the difference between

This

<?php
//Connect to MySQL
$con = mysqli_connect("localhost", "root", "root", "shoutit");

//Test Connection
if(mysqli_connect_errno()){
    echo 'Faild to connect to MySQL' .mysqli_connect_error();
}

?>

// And this

<?php

$db = new PDO("mysql:host=localhost;dbname=shirts4mike;port=8889","root","root");
var_dump($db);

?>

They both seem to do the exactl same job?

I used the first one to connect but i discovered that Randy uses this .

2 Answers

PDO (PHP Data Objects) is a object that can work with 12 different database system, Where as mysqli only works with mysqli Database.

So, if in future you decided to switch your project on another Database then PDO will make that task easy but with mysqli you have to write entire code again.

Only that's the main difference between them else they almost do same stuff you can use whatever you like.

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you for the answer.

I little questoin, when making a treehouse website like, e.g log in log out, news feed in forum , vidoes and categories , is phpmyadmin okay?

What would you reccomend me to use? im just starting out and im loooking at udemy course as same as here .

phpmyadmin is just a php script or its a free and open source tool written in php which makes it easy to use mysql Database from web browser. It can perform the task like creating, deleting or modifying database, tables, fields, rows, executing SQL statements and managing user permissions.

And yes most of the developer use phpmyadmin including me as it makes every task very easy.In short my answer is "yes" you must use phpmyadmin instead of using anyother tool or mysql manually.

Konrad Pilch
Konrad Pilch
2,435 Points

Oh, thats great ! I jsut remembered about excell . Why do people or when i was watching a tutorial , say that using excell with databse or something is needed to learn? or either it means that in excell you make tables and you jsut connect it with to the code . Though phpmyadmin is more powerful i believe so why people still use that or why is it in first place? I jsut remembered this from like a year ago from lynda . And yt aswell if im right . Thank you for the answer.

Konrad Pilch
Konrad Pilch
2,435 Points

Uh another think : p if that doesnt annoj you : p

Could you tell me why he did it this way now?

//Create connection credentials
$db_host = 'localhost';
$db_name = 'quizzer';
$db_user = 'root';
$db_pass = 'root';

//Create mysqli object
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);

Is it because he will use different types of these maybe or soemtighn in particular place or something? : p

im trying to learn things as i go along basicaly . I find it more interesting then starting of at treehouse though i comeback to treehouse as i got the basics and then improve my code as treehouse code is <3 while other people dont write such good code.

They both $mysqli = new mysqli() and mysqli_connect() serve the same purpose. They both connect to Mysql database server.

$mysqli = new mysqli(); // This create a new object called $mysqli as it is OOP Style usage. mysqli_connect(); // its a function which will connect with server.

I mentioned the object above, Object is collection of variables and functions in object functions are called methods and variables are called properties.

Now $mysqli->query() is a method which will run a query against mysql database this is same as mysqli_query().

You can use any of these OOP Style usage by creating mysqli object, mysqli_connect(), or by creating PDO object. They all serve the same purpose.

Konrad Pilch
Konrad Pilch
2,435 Points

What does $mysqli->query() stands for ? its a method but what query will it run ?