Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Konrad Pilch
2,435 PointsWhat 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
17,916 PointsThe data is sent to the database over the network( tcp/ip connection )

jrabello
17,916 PointsHello 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:

Konrad Pilch
2,435 PointsHi, 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
2,435 PointsDo you know what wrong i did with this code here ?
To me it seem everything like it should wokr but it doesnt.

Konrad Pilch
2,435 PointsJust solved it xd wow.. experience xd faliture is soo cool.
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsWhat i mean, if you want to insert data into the database, you allways write
Right? its a MUST ?
jrabello
17,916 Pointsjrabello
17,916 PointsYes, if you want to execute a query in the mysql database you need to call always mysqli_query
Konrad Pilch
2,435 PointsKonrad Pilch
2,435 PointsOkay. Thank you! I was confused : p