Don't forget to hit the ⭐ if you like this repo.
PHP (Hypertext Preprocessor) is a widely-used open-source scripting language specifically designed for web development. It is embedded within HTML code and executed on the server-side, generating dynamic web content. PHP can interact with databases, handle form data, and perform various server-side tasks.
PHP Example:
<?php
// Simple PHP code
$name = "John Doe";
echo "Hello, " . $name;
?>
In the above example, a variable $name
is assigned the value "John Doe". The echo
statement is used to display the concatenated string "Hello, John Doe" on the webpage.
MySQL is an open-source Relational Database Management System (RDBMS) that provides a reliable and efficient way to store, manage, and retrieve data. It uses Structured Query Language (SQL) for database operations such as creating tables, inserting data, querying data, and more. MySQL is widely used in conjunction with PHP to build dynamic web applications.
MySQL Example:
-- Simple MySQL query to create a table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)
);
In the above example, a table named "users" is created with three columns: id
, name
, and email
. The id
column is set as the primary key with auto-increment, and the name
and email
columns are defined as VARCHAR types to store text data.
<?php
// Connect to MySQL database
$host = "localhost";
$username = "root";
$password = "password";
$database = "mydatabase";
$conn = mysqli_connect($host, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Execute MySQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
// Display query results
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Email: " . $row['email'] . "<br>";
}
} else {
echo "No records found";
}
// Close database connection
mysqli_close($conn);
?>
In the above example, a connection to a MySQL database is established using PHP's mysqli_connect()
function. A SQL query is executed to select all records from the "users" table. The results are then displayed using a while
loop and the mysqli_fetch_assoc()
function. Finally, the database connection is closed using mysqli_close()
.
This is a basic example of integrating PHP and MySQL, showcasing how PHP can connect to a MySQL database, execute queries, and retrieve data from the database.
Remember to modify the database credentials ($host
, $username
, $password
, $database
) according to your specific setup.
CRUD (Create, Read, Update, Delete) operations are fundamental database operations used to manage data in a database system. In the context of PHP and MySQL, CRUD operations involve interacting with the MySQL database to create, retrieve, update, and delete records.
To create a new record in a database table, we use the INSERT statement in SQL.
Example PHP code for creating a new record:
<?php
// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Prepare data for insertion
$name = "John Doe";
$email = "[email protected]";
// Create SQL query
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
// Execute query
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
// Close database connection
mysqli_close($conn);
?>
In the above example, we connect to the MySQL database, prepare the data to be inserted (name and email), create an SQL query using the INSERT statement, and execute the query using mysqli_query()
. If the query is successful, a success message is displayed. Otherwise, an error message is shown.
To retrieve records from a database table, we use the SELECT statement in SQL.
Example PHP code for reading records:
<?php
// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Create SQL query
$sql = "SELECT * FROM users";
// Execute query
$result = mysqli_query($conn, $sql);
// Display query results
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Email: " . $row['email'] . "<br>";
}
} else {
echo "No records found";
}
// Close database connection
mysqli_close($conn);
?>
In the above example, we connect to the MySQL database, create an SQL query using the SELECT statement to retrieve all records from the "users" table, execute the query using mysqli_query()
, and display the results using a while
loop and mysqli_fetch_assoc()
.
To modify existing records in a database table, we use the UPDATE statement in SQL.
Example PHP code for updating a record:
<?php
// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Prepare data for update
$id = 1;
$email = "[email protected]";
// Create SQL query
$sql = "UPDATE users SET email='$email' WHERE id=$id";
// Execute query
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
// Close database connection
mysqli_close($conn);
?>
In the above example, we connect to the MySQL database, prepare the data to be updated (id and new email), create an SQL query using the UPDATE statement, and execute the query using mysqli_query()
. If the query is successful, a success message is displayed. Otherwise, an error message is shown.
To remove records from a database table, we use the DELETE statement in SQL.
Example PHP code for deleting a record:
<?php
// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Prepare data for deletion
$id = 1;
// Create SQL query
$sql = "DELETE FROM users WHERE id=$id";
// Execute query
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
// Close database connection
mysqli_close($conn);
?>
In the above example, we connect to the MySQL database, prepare the data for deletion (id), create an SQL query using the DELETE statement, and execute the query using mysqli_query()
. If the query is successful, a success message is displayed. Otherwise, an error message is shown.
These examples demonstrate the basic CRUD operations (Create, Read, Update, Delete) using PHP and MySQL. Modify the database connection parameters (host
, username
, password
, database
) according to your specific setup.
Please create an Issue for any improvements, suggestions or errors in the content.
You can also contact me using Linkedin for any other queries or feedback.