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

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

jrabello
jrabello
17,917 Points

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

Konrad Pilch
Konrad Pilch
2,435 Points

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 ?

jrabello
jrabello
17,917 Points

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

Konrad Pilch
Konrad Pilch
2,435 Points

Okay. Thank you! I was confused : p

jrabello
jrabello
17,917 Points

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

Konrad Pilch
Konrad Pilch
2,435 Points

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?

Konrad Pilch
Konrad Pilch
2,435 Points

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

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

Konrad Pilch
Konrad Pilch
2,435 Points

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