💾 Part 3: PHP and SQL syntax examples

Basic data insertion

// Get values from posted HTML inputs.
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];

// INSERT query
mysqli_query($con, "INSERT INTO tablename (name, age, gender) VALUES ('$name', '$age', '$gender')");

Basic data deletion

// Deletes all the rows from the database where column "name" has value "Mart".

$name = 'Mart';

mysqli_query($con, "DELETE FROM tablename WHERE name = '$name'");

Basic data selection

// Select all fields from specific rows from the database, to run for example loops in the site

mysqli_query($con, "SELECT * FROM tablename WHERE gender = 'male'");
// Basic data selection and using in the loop; Selecting records where gender is male and age is bigger than 28 (so minimum age is 29).

// Store the array in $query variable
$query = mysqli_query($con, "SELECT * FROM tablename WHERE gender = 'male' AND age > 28");

// Loop through the array
while($row = mysqli_fetch_array($query)) {
    echo $row['name'];
    echo ": ";
    echo $row['age'];
    echo "<br>";
}

// Result:
// Alex: 30
// Dave: 31
// Matt: 40

Scroll to Top