Logo
Unit 15 – Basic MySQL Interaction

Basic MySQL Interaction

Duration: 5 minutes

Hello to all aspiring PHP developers out there!

Welcome to a crucial aspect of web development: interacting with databases. In this module, we will delve into the basics of MySQL interaction using PHP. Databases are integral for storing, retrieving, and manipulating data, and PHP, combined with MySQL, provides a powerful tool for managing this data.

MySQL and PHP: A Dynamic Duo

MySQL is a popular open-source relational database management system. When used in conjunction with PHP, it allows for dynamic data-driven web applications. Whether you’re storing user data, product information, or blog posts, understanding how to interact with MySQL using PHP is a key skill.

Connecting to a MySQL Database

Before any data can be manipulated, a connection to the database must be established. PHP’s mysqli or PDO extension can be used for this purpose.

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

Performing Basic CRUD Operations

Create, Read, Update, and Delete (CRUD) operations form the backbone of database interaction.

  • Create: Inserting data into the database.
$sql = "INSERT INTO tablename (column1, column2) VALUES ('value1', 'value2')";
$conn->query($sql);

  • Read: Fetching data from the database.
$sql = "SELECT column1, column2 FROM tablename";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["column1"]. " - " . $row["column2"]. "";
}

  • Update: Modifying existing data
$sql = "UPDATE tablename SET column1='newvalue' WHERE condition";
$conn->query($sql);

  • Delete: Removing data from the database.
$sql = "DELETE FROM tablename WHERE condition";
$conn->query($sql);E

Exercise

Simple CRUD Application:

  • Create a Database and Table: If not already done, create a MySQL database and a table with a few fields using a MySQL client of your choice.
  • Insert Data: Use PHP to insert some data into your table.
  • Retrieve and Display Data: Fetch the data using PHP and display it on a web page.
  • Update Data: Choose an entry and update some of its fields using PHP.
  • Delete Data: Finally, delete an entry from your table using PHP.

Feel free to use JDoodle to test your PHP scripts, but remember that you will need a MySQL server to run the MySQL queries.

Conclusion

Being able to interact with databases is a cornerstone of web development. With the basic CRUD operations under your belt, you’re on your way to creating fully interactive and dynamic web applications. Remember, practice is key, so keep experimenting, keep coding, and watch as your web applications come to life!

You've completed this module!

Continue learning via other JDoodle's professional php Tutorials!

View Modules

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!