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 trialKonrad 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,917 PointsThe data is sent to the database over the network( tcp/ip connection )
jrabello
17,917 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,917 Pointsjrabello
17,917 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