Executing query in PhpMyAdmin using PHP

Executing query in PhpMyAdmin using PHP

In our last post, we learned how to connect PhpMyAdmin using PHP. In this post, we will learn how to execute four of the query and in the other post how to create a database.

We have saved our database configuration file on "database.php". Today we will create a new file "execute.php" to execute the query.

To execute the query, use the given code below and run the "execute.php" file on the browser one by one starting with insert.

Insert Query

To execute the insert query, we will declare some variables and insert them into the database. Later on, we need to get these values from form other any other components using post or get methods.

include 'database.php';
// INSERT query to add a new record to the 'data' table
$name = "John";
$phone = "1234567890";
$email = "john@example.com";
$message = "Hello, World!";

$sql = "INSERT INTO data (name, phone, email, message) VALUES ('$name', '$phone', '$email', '$message')";
if ($mysqli->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $mysqli->error;
}

If the query is executed successfully, a message saying "New record created successfully" and for error, a MySQL error message will be displayed.


Select Query

// SELECT query to retrieve all records from the 'data' table
$sql = "SELECT * FROM data";
$result = $mysqli->query($sql);

// Check if there's an error in the query
if (!$result) {
  echo "Failed to retrieve data: " . $mysqli->error;
  exit();
}

// Display the data in a table format
echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Phone</th><th>Email</th><th>Message</th></tr>";
while ($row = $result->fetch_assoc()) {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['phone'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['message'] . "</td>";
  echo "</tr>";
}
echo "</table>";

Update Query

For now, we are using the value of id as 1. If executing the below mentioned query throws any error or doesn't update, please check the value of id matches with the database or not.

// UPDATE query to modify an existing record in the 'data' table
$id = 1;
$name = "Jane";
$sql = "UPDATE data SET name='$name' WHERE id=$id";
if ($mysqli->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error: " . $sql . "<br>" . $mysqli->error;
}

Delete Query

For now, we are using the value of id as 2. If executing the below mentioned query throws any error or doesn't get deleted, please check the value of id matches with the database or not.

// DELETE query to remove a record from the 'data' table
$id = 2;
$sql = "DELETE FROM data WHERE id=$id";
if ($mysqli->query($sql) === TRUE) {
  echo "Record deleted successfully";
} else {
  echo "Error: " . $sql . "<br>" . $mysqli->error;
}

In our next episode, we will learn to execute the query with the help of buttons and other HTML elements and manipulate the database values on HTML pages.

Did you find this article valuable?

Support Shital Mainali by becoming a sponsor. Any amount is appreciated!