Php MySql Basic Tutorial

PHP

Posted by indranil on Jan. 25, 2021, 4:59 p.m.

PHP TO MYSQL BASIC TUTORIAL:

MySQL is a database system used on the web. MySQL is a database system that runs on a server. MySQL is ideal for both small and large applications. MySQL is very fast, reliable, and easy to use. MySQL uses standard SQL. MySQL compiles on a number of platforms. MySQL is free to download and use.

#MySql DataBase Connection On Localhost:

<?php
// Connecting to the Database
$servername = "localhost";
$username = "root";
$password = "";
$database = "your_db_name";

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Die if connection was not successful
if (!$conn){
    die("Sorry we failed to connect: ". mysqli_connect_error());
}
else{
    echo "Connection was successful<br>";
}

?>

#MySql Database To Insert ObjectQuery 

<?php

// sql query to the execute


$sql="INSERT INTO `users786` (`first_name`, `last_name`, `email`, `mo_no`, `password`, `re-enter_password`, `age`, `gender`, `dt`) VALUES ('$firstname', '$lastname', '$email', '$number', '$password', '$c_password', '$age', '$gender', current_timestamp())";


$result = mysqli_query($conn, $sql);
$result = mysqli_query($conn, $sql);

if($result)
{
    echo'<div class="alert alert-success alert-dismissible fade show" role="alert">
    <strong>Success!</strong> your entery has been submitted successfully.
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>';
}
else{
    echo "Not Submit of this error is>>>". mysqli_error ($conn);
}

?>

#MySql Database To Fetch ALL ObjectQuery 

<?php

 include 'your_database_config_path';

$sql = "SELECT * FROM `phptrip`";
$result = mysqli_query($conn, $sql);

// Find the number of records returned
$num = mysqli_num_rows($result);
echo $num;
echo " records found in the DataBase<br>";
// Display the rows returned by the sql query
if($num> 0){

    //One By One Show Object_Query
    // $row = mysqli_fetch_assoc($result);
    // echo var_dump($row);
    // echo "<br>";

    // We can fetch in a better way using the while loop
    while($row = mysqli_fetch_assoc($result)){
        // echo var_dump($row);
        echo $row['sno'] .  ". Hello ". $row['name'] ." Welcome to ". $row['dest'];
        echo "<br>";
    }


}
?>

MySql To Database Fetch Filter Objects Query

<?php
$sql
= "SELECT * FROM `phptrip` WHERE `dest`='Bihar'"; $result = mysqli_query($conn, $sql); // Usage of WHERE Clause to fetch data from the database $num = mysqli_num_rows($result); echo $num . " records found in the DataBase<br>"; $no=1; if($num> 0){ while($row = mysqli_fetch_assoc($result)){ echo $no . ". Hello ". $row['name'] ." Welcome to ". $row['dest']; echo "<br>"; $no = $no +1; }
?>

MySql To Database Update Objects Query

<?php
// Usage of WHERE Clause to Update Data

$sql
= "SELECT * FROM `phptrip` WHERE `dest`='Bihar'"; $result = mysqli_query($conn, $sql);
$sql = "UPDATE `phptrip` SET `name` = 'FromBihar' WHERE `dest` = 'Bihar'"; $result = mysqli_query($conn, $sql); echo var_dump($result); $aff = mysqli_affected_rows($conn); echo "<br>Number of affected rows: $aff <br>"; if($result){ echo "We updated the record successfully"; } else{ echo "We could not update the record successfully"; } ?>

#MySql To Database DELETE Objects Query

<?php
$sql = "SELECT * FROM `phptrip` WHERE `dest`='Bihar'"; $result = mysqli_query($conn, $sql);$sql
= "DELETE FROM `phptrip` WHERE `dest` = 'Russia' LIMIT 13"; $result = mysqli_query($conn, $sql); $aff = mysqli_affected_rows($conn); echo "<br>Number of affected rows: $aff <br>"; if($result){ echo "Delete successfully"; } else{ $err = mysqli_error($conn); echo "Not Delete successfully due to this error --> $err"; } ?>
 

noimg Visit Link: digitalocean.com