Thursday, 2 March 2017

MySQL Insert Record



Table Structure

Field
Type
Null
Default
Comments
roll
int(11)
No 

 Auto Increment
name
varchar(50)
No 


city
varchar(50)
No 


sub
varchar(30)
No 






MYSQLi Object Orieted Approach


<?php 
$servername = "localhost";
$username = "root";
$password = "";

$dbname = "tmp";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);


$sql = "INSERT INTO stu values('NULL','sona','vns','Hindi')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

MYSQLi Object Orieted Approach


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tmp";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO stu values('NULL','Rajesh','Varanasi',''Art)";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

?>