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

PHP What does this means?

Hello,

Could osmebody walk me throw this code ? mainly im confused with this code here i mean is the $mysqli and the arrrow -> assigning it like in the array the name and then the value?

//Get result
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

    $question = $result->fetch_assoc();
    //Set Question number
    $number = (int) $_GET['n'];

    //
    //Get Question
    //

    $query = "SELECT * FROM `questions`
                WHERE question_number = $number";

    //Get result
    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);

    $question = $result->fetch_assoc();

ANd this is all the code in one.

As im builidng a quizzer.

3 Answers

its object oriented php for the query. so the object is $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); its saying this object. php has to log into my sql and then run sql script you can generate this with php but you have to run it in the database it then returns data from the data base to php. you can look at and read some about it here (http://php.net/manual/en/mysqli.query.php)

in the second section of the page they talk about Procedural style and the top section talks about Object oriented style

Konrad Pilch
Konrad Pilch
2,435 Points

maybe i do get it better now and i may think i know kind of how or when to use it . Thank you!

it is setting the results of the query to the database as $result and if it can not do this stopping the operation and returning the error for the reason it could not be completed. if its completed it takes the results of the query and puts it in an associative array with the variable name $question. you can go through database-foundations and PHP & Databases with PDO to get a more in depth understanding of this if you want.

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you!

So what does this stands for?
$mysqli->query($query)

Ill check them out .

its a php/sql command to query the database. example: $mysqli->query("SELECT Name FROM City LIMIT 10"). they have taken the "SELECT Name FROM City LIMIT 10" and set it as a variable $query and passed it as an argument so $result = $mysqli->query($query); takes the query and sets it as the variable of $result.

Konrad Pilch
Konrad Pilch
2,435 Points

what i more mean why is there a -> , this arrow is confusing me all the time when i see anything with it .

I know that the query as you said its a build in command to query database and the $query is the "SELECT * FROM questions"; but whats up with the arrow? im going throw database foundation course atm. I did some other course, where Randy talked about it i think, but i didnt get it.