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

What is this SQL??

HI, i do understand the code and everything apart one thing.

$sql = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('John', 'Rambo', 'johnrambo@mail.com')";

How is this wokring?? what is this sql ?

This is all the code :

<?php

$con = mysqli_connect("localhost", "root","root", "dbname");

// Check connection
if($con === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt insert query execution
$sql = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('John', 'Rambo', 'johnrambo@mail.com')";
if(mysqli_query($con, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
}

// Close connection
mysqli_close($con);
?>

3 Answers

The data is sent to the database over the network( tcp/ip connection )

What i mean, if you want to insert data into the database, you allways write

$querry = "INSERT INTO persons (first_name, last_name, email_address) VALUES ('John', 'Rambo', 'johnrambo@mail.com')";

if(mysqli_query($con, $querry)){
    echo "Records added successfully.";
}

Right? its a MUST ?

Yes, if you want to execute a query in the mysql database you need to call always mysqli_query

Okay. Thank you! I was confused : p

Hello Konrad,

mysqli_query() is a php function that sends the SQL query to the mysql database, if you're having problems to understand it you should think about doing this couse first:

http://teamtreehouse.com/library/database-foundations

Hi, i dont mean that, i mean the variable $sql, how is this added to the database?

This code :

if(mysqli_query($con, $sql)){
    echo "Records added successfully.";

Oh i get it, the variable is random , i couldv just write query and then the mysqli_query sends the data to the database so if i want to add something or delete, i need to use mysqli query right?

Do you know what wrong i did with this code here ?

To me it seem everything like it should wokr but it doesnt.

Just solved it xd wow.. experience xd faliture is soo cool.