3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.
com
Menu
Ebhor.com
Knowledge Never Sets
PHP MySQL CRUD Tutorial with MySqli and
PHPMyAdmin
PHP MySQL CRUD involves connection of PHP with MySQL database and
performing Create (Insert), Read (Select), Update (Modify) and Delete
Operation on table.
PHP and MySQL connectivity is a very important topic while learning PHP
here step by step PHP and MySQL connectivity and create update delete
and read operations are discussed using mysqli and using PHP Data
Objects (PDO) are discussed.
To create database and tables PHPMyAdmin are used.
MySQL is a freely available open source Relational Database management
System (RDBMS)** that uses Structure Query Language (SQL).
SQL is the most popular language to accessing, adding and managing
content in a database.
https://www.ebhor.com/php-and-mysql/ 1/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
It is popular because its fast process, reliable, ease and flexible of use.
Menu
PHP can connect to MySQL and manipulate database. It is used on the
server.
Create any PHP application need MySQL database to managing data.
MySQLis an important part of most of all PHP application like WordPress,
Joomla, Magento and Drupal.
MySQLis a one kind of database system for web application with huge size
of storage space (like Facebook, Twitter and Wikipedia)
MySQL are stored data in tables. A table is a collection of related data, one
kind of array.
But PHP can store data and managing data except MySQL by using another
database like ODBC (Open Database Connectivity).
Note: **RDBMS is a collection of data, stored and accessed virtually.
RDBMS is used for the definition, querying, creation, update and
administration of databases.
It is a software application to interact with user, other application means
programming language (PHP, JAVA, .NET, Python etc.) and also database
https://www.ebhor.com/php-and-mysql/ 2/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
itself analyze data. Menu
Create Database on phpMyAdmin
In the XAMPP OR WAMPP server phpMyAdmin is present for MySQL.
Open XAMPP or WAMPP control panel start Apache and MySQL. Then
enter localhost/phpmyadmin on the browser url.
Open phpMyAdmin section which is the main MySQL database.
Here database, table can create and also data stored and manage.
Click on the Database option then open a text box to create new database
with database name.
https://www.ebhor.com/php-and-mysql/ 3/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Menu
Enter database name in the open textbox and click the Create button.
Remember one thing never used space between two words of database
name only used – (dash) or _ (underscore) and MSQL is case sensitive so
database name and table name field name are use same as it is.
If user create database with same name (previous any other existing
database name) then new database is not create.
Always set database name similar with application (like create school
management system then create database for this application with
school_management name).
When a database is created successfully then this database shown in the
left side list with alphabetical order.
https://www.ebhor.com/php-and-mysql/ 4/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Click the database then open particular
Menu
those database table list or if there
has no table then show below image.
In MYSQL there has no limit to create database.
PhpMyAdmin creating table
Create Table on phpMyAdmin
Create table in a particular database is very simple like create new
database.
When any user open their database there has an option create table a blank
text box there enter table name and 2,3,4 any number in the next box
(Number of columns) and click the go button.
Table name rules is same as database name never using space between
two words of table name.
https://www.ebhor.com/php-and-mysql/ 5/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Menu
Creating table in PHPMyAdmin
There is a student table with 5 numbers of columns. Here columns are field
name like student name, address, marks, rollnumber, phone, etc.
Entered name value in the name column like(name, address, marks,
rollnumber, phone, etc.)
Then select type of particular column in the type column.
Here type means which type of data stored in this field (likename is text
value so choose text, address is alphanumeric so choose varchar and phone
is numeric so choose int etc)
Then enter length means how many character or digit stored in this column.
(like int support 0 to 11 here only store maximum 9 digit because + and – are
by default store 2 digits so if any user store number greater than 9 then
https://www.ebhor.com/php-and-mysql/ 6/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
choose bigint which can store 20 digit maximum.
Menu
Varchar has 255 character limits. Text has no limit; in text type no need to set
length value)
Then set primary key for this table in the index column and click in the save
button.
In this way a simple table will be created in the database.
No limitation for create table in one database.
User can create many table in one database as their require.
MySql Table Structure
PHP MySql Connection
https://www.ebhor.com/php-and-mysql/ 7/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Before Performing PHP MySQL CRUD we have to connect PHP with MySql
Menu
Database.
There have some method to connect PHP application with MYSQL
database. Like MYSQL, MYSQLi and PDO etc.
Now days MYSQLi is mainly used for connecting database.
Syntax of MYSQLi:
connection.php : PHP MySql Connection Program Example
1 <?php
2 $conn = new MYSQLi("localhost","root",null,"tutorial");
3 if(mysqli_connect_error()){
4 die(mysqli_connect_error());
5 }
6 ?>
Explain:
MYSQLi is a library class for MYSQL database. Using this class manage
MYSQL database in a PHP application.
There have many functions to display data, store data, delete data and
update data of database.
https://www.ebhor.com/php-and-mysql/ 8/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
In the above example first parameter
Menu
is server host name where the
database is present by default local server host name is localhost.
Second parameter is server user name which is root in local server.
Third parameter is password value which is null in local server.
Forth parameter is database name means which particular database is
connected with this PHP application.
Here $conn return object of MYSQLi class of particular tutorial database.
mysqli_connect_error() is a pre-define function which check the connection
is established successfully or not.
If host name user name password database name are wrong then it returns
error the stop the PHP execution.
PHP MySQL Insert Data in table
First operation of PHP MySQL CRUD is Create or Insert.
Lets see How to insert Data in MySql Using PHP.
create.php PHP MySQL Insert Program example
1 <html>
https://www.ebhor.com/php-and-mysql/ 9/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
2 <body>
Menu
3 <?php
4 include "connection.php";
5 if( isset($_POST["name"])){
6 $name = $_POST['name'];
7 $phone = $_POST['phone_number'];
8 $address = $_POST['address'];
9 $marks = $_POST['marks'];
10
11 $sql = "INSERT INTO `student`(`name`, `phone`, `address`, `marks`) VALUES ('$name','$ph
12 $conn->query($sql);
13 echo "One record Insert";
14 }
15 ?>
16 <center>
17 <h1>Add Student Record</h1>
18 <hr>
19 <form method="post" action="">
20 <table border = 1 cellpadding = 15 width = 350 height = 175>
21 <tr>
22 <td>Name</td>
23 <td><input type = "text" name = "name" placeholder = "Enter Student Name"/></td>
24 </tr>
25 <tr>
26 <td>Phone</td>
27 <td><input type = "number" name = "phone_number" placeholder = "Enter Student Phone Numbe
28 </tr>
29 <tr>
30 <td>Address</td>
31 <td><textarea name = "address" placeholder = "Enter Student Address"></textarea></td>
32 </tr>
33 <tr>
https://www.ebhor.com/php-and-mysql/ 10/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
34 <td>Marks</td>
Menu
35 <td><input type = "number" name = "marks" placeholder = "Enter Student Marks"/></td>
36 </tr>
37 <tr>
38 <td colspan = 2 align = "right"><input type = "submit" value = "Save" /></td>
39 </tr>
40 </table>
41 </form>
42 </center>
43 </body>
44 </html>
Output
Add Student Recored (create.php)
https://www.ebhor.com/php-and-mysql/ 11/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Menu
PHPMyAdmin Showing student record
Explain:
MYSQL gives insert query for store data in the database.
1 Insert into `tablename`(`column1`,`column2`,`column3`,`column4`,`column5`)values(‘value1’,‘
Insert, into and values are keywords user can write it in any case small or
caps.
Table name and column name are case sensitive so write same as define.
Execute any MYSQL query used query() function. In above example $sql
variable store insert query that’s insert query is executed and one data is
stored in the database table.
query() function is call by MYSQLi connection object so need to include
database connection file.
https://www.ebhor.com/php-and-mysql/ 12/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Without database connection neverexecute
Menu
any MYSQL query.
PHP MySQL Select from Table
Another operation of PHP MySQL CRUD is Read or Select.
Lets see how to Select or read MySql Data in PHP.
show.php PHP MySQL Select program example
1 <html>
2 <body>
3 <?php
4 include "connection.php";
5 ?>
6 <center>
7 <h1>All Student Record</h1>
8 <hr>
9 <table border = 1 cellpadding = 15 width = 600 height = 300>
10 <tr>
11 <th>Roll Number</th>
12 <th>Name</th>
13 <th>Phone</th>
14 <th>Address</th>
15 <th>Marks</th>
16 </tr>
17 <?php
18 $sql = "SELECT * FROM `student`";
19 $res = $conn->query($sql);
20 while($rows = $res->fetch_array()){
21 echo "<tr>
https://www.ebhor.com/php-and-mysql/ 13/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
22 <td>$rows[rollnumber]</td>
Menu
23 <td>$rows[name]</td>
24 <td>$rows[phone]</td>
25 <td>$rows[address]</td>
26 <td>$rows[marks]</td>
27 </tr>";
28 }
29 ?>
30 </table>
31 </center>
32 </body>
33 </html>
Output:
Showing all students record in show.php
MYSQL gives select query to fetch data from database.
1 select * from `tablename`
https://www.ebhor.com/php-and-mysql/ 14/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Here * return all columns value of this table.
Menu
If any user return only specific some column then he/she can write select
query like this way, select name , phone , marks from student .
Here only name, phone and marks value return.
In above example query() function execute select query so student table
selected.
Now display the student table’s data, use fetch_array() function which return
at a time one row of the table.
If user show the all record of the table then call the fetch_array() in a loop.
In above example use while loop for fetch all record of the student
table.fetch_array() function is return table data as a numeric array and
associative array both way.
So user can print name value like $rows[name] or $rows[1] both return same
result.
PHP MySql delete from table
Another operation of PHP MySQL CRUD is Delete or Remove.
MYSQL gives delete query for delete data from database.
https://www.ebhor.com/php-and-mysql/ 15/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
delete from tablename . This querydeletes
Menu
all record of the table. Table will
be empty after execute the query.
If user want to delete particular one row then add where clause in the delete
query. MYSQL support where clause and also AND OR clause.
delete.php PHP MySQL Delete Program Example
1 <?php
2 include "connection.php";
3
4 $sql = "DELETE FROM `student` WHERE `rollnumber` = 1";
5 $conn->query($sql);
6 echo "Student record deleted";
7 ?>
Output:
Student record deleted
Showing record after deleting first record
https://www.ebhor.com/php-and-mysql/ 16/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Now in the student table, only one record
Menu
left.
The first row deleted from the student table. Always use the primary key
column in the where clause. Because the primary key field is store unique
value (no duplicate data and no null value) that’s why only exact data will be
deleted from the table.
If any user use another column in where clause then maybe more than one
row will be deleted if value is same in another rows.
Like user use name column then if more than one student name are same
then more than one record will be deleted.
PHP MySQL Update from table
Another operation of PHP MySQL CRUD is Update or Edit.
MYSQL gives update query to update data in the table.
1 update `tablename` set `column1` = ‘value1’,`column2` = ‘value2’,`column3` = ‘value3’
With this query column1, column2 and column3 value are modified by new
value for all rows in the table.
If user want to update particular one row then use where clause condition in
the update query.
https://www.ebhor.com/php-and-mysql/ 17/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
update.php PHP MySQL Update Program
Menu
Example
1 <?php
2 include "connection.php";
3
4 $sql = "UPDATE `student` SET `address` = 'Sonarpur', `marks`= '99' WHERE `rollnumber` = 2"
5 $conn->query($sql);
6 echo "Student record updated";
7 ?>
Output:
Student record updated
In the above example address and marks value modified in the one row
where rollnumber value is 2.
PHP MySql Insert multiple rows in table
PHP MySQL Insert multiple rows program example
1 <?php
2 $conn = new MYSQLi("localhost","root",null,"test");
3 if($_POST['submit']){
4 if(count($_FILES['upload']['name']) > 0){
5 for($i=0; $i<count($_FILES['upload']['name']); $i++) {
6 @mkdir("uploaded");
7 $filePath = "uploaded/".time().'-'.$_FILES['upload']['name'][$i];
8 copy($_FILES['upload']['tmp_name'][$i],$filePath);
9 $sql="insert into `file` (`id`,`filepath`) values ('','$filePath')";
10 $conn->query($sql);
https://www.ebhor.com/php-and-mysql/ 18/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
11 }
Menu
12 }
13 }
14 ?>
15
16 <form action="" enctype="multipart/form-data" method="POST">
17 <input name="upload[]" type="file" multiple/><br>
18 <input type="submit" name="submit" value="Submit">
19 </form>
File uplaod form
Uploaded files
Note:
multiple keyword used to select multiple file at a time.
PHP MySqli Last Insert Id
https://www.ebhor.com/php-and-mysql/ 19/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
PHP MySQL select last insertid program
Menu
example
<?php echo $conn->insert_id;?>
Its return last auto increment column value. In the file table last inserted id
value is 4 so insert_id return 4.
Call the insert_id function with the database connection object and write the
code after insert query.
Set a column to auto increment value in tableto check A_I option in the table
structure.
Column with auto increment
Select A_I to make column auto increment
PHP PDO prepared statements
https://www.ebhor.com/php-and-mysql/ 20/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
PDO (PHP Data Objects) is the one of the database connection method to
Menu
connect MySQL database to PHP application.
In this method insert query needs to prepare before execute.
prepareinsert.php :PHP PDO MySql Data Insert example
1 <?php
2 //Databse connection
3 $servername = "localhost";
4 $username = "root";
5 $password = "";
6 $conn = new PDO("mysql:host=$servername;dbname=tutorial", $username, $password);
8 //Insert Data in the table with prepare statement
9 $name = "Biplab Bagchi";
10 $phone = "8877665544";
11 $address = "Sodhpur";
12 $marks = "60";
13
14 $sql = $conn->prepare("INSERT INTO `student` (`name`, `phone`,`address`,`marks`) V
15 $sql->bindParam(1, $name);
16 $sql->bindParam(2, $phone);
17 $sql->bindParam(3, $address);
18 $sql->bindParam(4, $marks);
19 $sql->execute();
20 ?>
https://www.ebhor.com/php-and-mysql/ 21/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Menu
Data inserted in table
Explain:
Prepared statements are very useful against SQL injections.
A prepared statement is a feature used to execute the same SQL
statements repeatedly with high efficiency.
Prepare: A SQL is created query and data sent to the database. Some
values are not specified, they are called parameters.
In above example INSERT INTO student VALUES (?, ?, ?, ?, ?) those are
parameters.
Database parses the data then compile and finally execute the SQL query, in
prepare method data stores in the server without execute.
https://www.ebhor.com/php-and-mysql/ 22/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Execute: In the execute method data combine with parameters and SQL
Menu
execute the query to store data into database.
User can execute the query as many times as they want with different
values.
Compared to executing SQL statements directly, prepared statements
have two main advantages
1. Prepared method reduces query parsing time for preparing a SQL
query.
2. Bind param minimize bandwidth to the server so website can be load
fast and only need to send parameter each time not the whole query.
With Prepared methods SQL query is secured from SQL Injection, because
bind param combined the values first then send to the database so no need
to be escaped correctly.
If the original values are not derived from input then SQL Injection con not
occurs.
php
mysql, php
How to use CKEditor in PHP Installation and Uses
Java Program Structure and First Program
https://www.ebhor.com/php-and-mysql/ 23/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Menu
PHP Introduction
PHP Comments
PHP Variables
PHP Echo and print
PHP Data Types
PHP Operators
PHP if else
PHP Switch Case
PHP for loop
PHP While loop
PHP Do While loop
PHP Form Handling
PHP String
PHP String Functions
PHP Array
PHP Array Types
Functions in PHP
PHP Date and Time
PHP Email Sending
PHP File Upload Example
PHP Session
PHP Session Example
PHP Cookies
PHP File Handing
https://www.ebhor.com/php-and-mysql/ 24/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
PHP Directories
Menu
PHP Constants
PHP Class and Objects
PHP Constructor and Destructor
PHP Inheritance
PHP Final Class
PHP MySQL
PHP PDO
PHP Polymorphism
PHP Regular Expressions
How to use CKEditor in PHP Installation and Uses
PHP Creating Image
https://www.ebhor.com/php-and-mysql/ 25/26
3/6/2021 PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin - Ebhor.com
Menu
About Us Contact Us Thanks to Careers Disclaimer Careers Disclaimer Log In
Register
© 2021 Ebhor.com • Built with GeneratePress
https://www.ebhor.com/php-and-mysql/ 26/26