Blog Introductions
In this blog contains tutorials about HTML Tutorials, Python Tutorials, CSS Tutorials, How to, PHP Tutorials, Java Tutorials, C++ Tutorials, Tutorials, Examples, Source code,Learning,
Tips and Software development services. Php with mysql database | php mysql crud operation | Php tutorials Bestitworriors
Php with mysql database
Php with mysql database | php mysql crud operation | Php tutorials Bestitworriors -Mysql is most popular database which use with php and easy to work with it.Mysql database used on website for holding the website data.We can perform various operation on php with mysql database we can insert data into the database and select data from database and show into out website and we can update data into the database and we can delete data from database.we will perform all these function with php mysql database.
Some Key Points of Mysql database
- Mysql hold data for a website
- First we make connection of php with the database
- After making connection we get the database object
- Now we will write query of insert function and send data
- Now we will write query of Select function and get data
- Now we will write query of Update function and send new data
- Now we will write query of Delete function and Delete data
- Data base can hold all kind of information like images,videos and text and files
Code
<?php
// Connection
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "StudentRecord";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert Data
$sql = "INSERT INTO Students (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
//SELECT Data
$sql = "SELECT id, firstname, lastname FROM Students";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
// Delete Data
$sql = "DELETE FROM Students WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
// Update Data
$sql = "UPDATE Students SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
0 Comments