following all the step to perform CRUD Operation using PHP Database Connection
<?php
//write to the database
/*
following all the step to perform CRUD Operation using PHP
Database Connection
step-1: create the database connection
mysqli_connect($host,$user,$pass,$dbname);
Retuen $connection_str or $Connection Object
$con = mysqli_connection($host,$user,$pass,$dbname);
$con = mysqli_pconnection($host,$user,$pass,$dbname);
|
persistant Connection
(Permanent Connextion)
#dbconnect.php
since connection is made only once
$host = '127.0.0.1' ; or localhost;
$user = 'root';
$pass = '';
$dbname = '' ; no Error
$dbname = 'dbname'; #database Name is correct then ok if
#Wrong Exception will be raised
Note : i here means Improved Verssion;
step-2 :
Check if connection is made or not
Method 1:
try{
$con = mysqli_connect($host,$user,$pass,$dbname);
echo 'connection Created';
}catch(Exception $e){
if(!con){
echo("Connextion Error".mysqli_error($con));
}
}
Method 2:
if(!con){
echo 'Created Connection';
}else{
echo "Connection Error ".mysqli_error($con);
exit;
}
Method 3:
$con = mysqli_connect($host,$user,$pass,$dbname)
step- 3 :
Prepare the Query and Fire The Query
#How to Prepare Query
$sql = "INSERT OR UPDATE OR SELECT QUERY OR DELETE QUERY";
#Fire the Query
1.writable
insert,delete, update, alter, create, drop, truncate....
mysqli_query($con,$sql)
| |-->sql
|----->connection Object
return type: true|false
2.Readable
select,joins,projection,selections.transaction
mysqli_query($con,$sql)
| |----->sql
|--------->conection Object.
return type : Result Set :: Multi -Dimensional Array of N records.
Syntax :
$result = mysqli_query($con,$sql);
Note :
$sql = "Query 1;";
$sql = "Query 1;";
$sql = "Query 1;";
$sql = "Query 1;";
mysqli_query($con,$sqli);
step- 4 :
if it is writeable or readable
if query is readable return type will be Result Set
select = Result set
if Query is Writable
return type no of affected Rows
minimum Affected rows
1=>true
if ($check==true){
echo 'Okay';
}else{
echo 'Can't Okay';
}
*/
All these function are use when you want to get the data
Readable ::Select
Q1. mysqli_num_rows()
Q2. mysqli_fetch_assoc()
Q3. mysqli_fetch_array()
Q4. mysqli_fetch_row()
Q5. mysqli_fetch_field()
All these function will used during write Operation
*update or delete
Q6. mysqli_affected_rows() :: Number of affected rows in case of delete and update.
-1 0 1
-1 0 (n)
min One affected => True.
Max n affected => True.
check condition (-1):
#true when Query is mysqli_fetch_field
$check_count = mysqli_affected_rows($con);
if($check_count>0){
}
*insert
Q7. mysqli_insert_id()
gives the Id of last inserted Record
select max(id) from any_table
Postmortam of Result set Functions :
#################################
1. mysqli_num_rows()
2. mysqli_fetch_assoc()
3.mysqli_fetch_array()
4.mysqli_fetch_row()
5.mysqli_fetch_field()
This Result set is also called Resource and denoted by #n
where n=1,2,3,4,5,6,.........................
$con :: is responsible for profiling
SQL Query
Explain <Any-Query>
In Order to check the Unique wheather a Record exits or not
we use $count>0 record exist.
Select Query ::
$count=mysqli_num_rows($result_set);
0 Comments