PHP MySQL Database tutorial
Using PHP with MySQL
In this blog, you are going to learn how PHP communicates with MySQL databases. It is common for every web developer to design a database and develop the code to manage data in the database. Once a database is created, you need to do several operations on it such as creating tables, inserting data and updating and deleting and so on. PHP has its own built-in functions to work on data that is stored in a database. The below code examples explains how to perform each operation using PHP. This tutorial requires knowledge of SQL queries. if no idea on SQL, please visit:
In this tutorial, we are not using any front-end coding. Using only PHP and Database in order to keep the logic simple and straightforward.
Table of Contents:
PHP MySQL - CREATE
PHP MySQL - READ
PHP MySQL - INSERT
PHP MySQL - UPDATE
PHP MySQL - DELETE
Managing MySQL Database in PHP
To work with MySQL Database, PHP provided the following functions.
- mysqli_connect() : Connects to Server
- mysqli_query($SQL) : Executes sql-query and returns result
- mysqli_close() : Closes opened connection
Syntax:
$conn = mysqli_connect("servername", "username", "password", "databasename")
CREATE
Databasename: usersdb
Query for Database :
CREATE database usersdb;
Query for Table :
Create a table register with the given code below, in the database: usersdb.
CREATE TABLE `register` ( `id` int(11) NOT NULL, `username` varchar(50) NOT NULL, `password` varchar(15) NOT NULL, `mobile` varchar(10) NOT NULL, `email` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Below example will add new rows to database.
Click here To view how to run the Queries in PhpMyAdmin
Below, code examples explain how to work with MySQL and PHP.
In the XAMPP server, create a folder and save all the files and run one by one.
Connect to Database in PHP:
Once the database created you need to create a connection with data database.
Then you can perform any operations(read, insert,update, delete)on it.
Here is the code to test connection with the database.
connection.php
<?php
$conn = mysqli_connect ("localhost", "root", "", "usersdb");
if ($conn)
{
print "<h2>Database Found ";
}
else
{
print "<h2>Database NOT Found ";
}
mysqli_close($conn);
?>
READ
This code example will read the data from the table that you created.
display_rows.php
<?php
$conn = mysqli_connect("localhost", "root", "", "usersdb");
if($conn){
$SQL = "SELECT * FROM register";
$result = mysqli_query($conn, $SQL);
while ($db_fields = mysqli_fetch_assoc($result)) { print $db_fields['username'] . " "; print $db_fields['password'] . " ";
print $db_fields['mobile'] . " "; print $db_fields['email'] . "<br>";
}
}
else
{
print "<h2>Database NOT Found ";
}
mysqli_close($conn);
?>
INSERT
Below example will add new rows to database.
add_row.php
<?php
$username="testuser";
$password ="pass";
$mobile = "9991234999"];
$email ="testuser@example.com";
$conn = mysqli_connect("localhost", "root", "", "usersdb");
if($conn)
{
$SQL = "INSERT INTO register(username, password, mobile, email)
VALUES ('$username','$password','$mobile','$email')"; $result = mysqli_query($conn, $SQL); print "Record added"; } else { print "Database NOT Found "; }
mysqli_close($conn);
?>
UPDATE
Below example updates a row in database.
update_row.php
<?php
$username = 'testuser';
$oldpassword = 'pass';
$newpassword = 'test123';
$conn = mysqli_connect("localhost", "root", "", "usersdb");
if ($conn) {
$SQL = "UPDATE register SET password = '$newpassword' WHERE password=
'$oldpassword' AND username = '$username'";
$result = mysqli_query($conn, $SQL);
print "Record updated to the database";
}
else
{
print "Database NOT Found ";
mysqli_close($conn);
}
?>
DELETE
This code example deletes a row in database.
First, it checks if the given row is available. If it is there, then it deletes.
If not found, displays a message.
delete_row.php
<?php
$username= 'testuser';
$conn = mysqli_connect("localhost", "root", "", "usersdb");
if ($conn) {
$result = mysqli_query($conn, "SELECT * from register where
username='$username'");
$num_rows=mysqli_num_rows($result);
if($num_rows==0) { die('Could not find User'.mysqli_error()); } else { mysqli_query($conn, "DELETE FROM register WHERE username='$username'") or die (mysqli_error());
echo "<h1>Record Successfully Deleted..!!</h1>";
}
}
else
{
print "Database NOT Found ";
}
mysqli_close($conn);
?>
Comments
Post a Comment