Friday, 29 July 2016

mysqli

PHP MySQLi Introduction:


PHP MySQLi = PHP MySQL Improved!

The MySQLi functions allows you to access MySQL database servers.

Note: The MySQLi extension is designed to work with MySQL version 4.1.13 or newer


The MySQLi Extension (MySQL Improved) is a relational database driver used in the PHP programming language to provide an interface with MySQL databases.
There are three main API options when considering connecting to a MySQL database server:
  • PHP's MySQL Extension
  • PHP's MySQLi Extension
  • PHP Data Objects (PDO)
   The MySQLi extension provides various benefits with respect to its predecessor, the most prominent of which, (according to the PHP website, ) are:
  • An object-oriented interface
  • Support for prepared statements
  • Support for multiple statements
  • Support for transactions
  • Enhanced debugging support
  • Embedded server support
  • More powerful Functionality


=====================mysqli program code example=====================

(1)create table   myrecord :
create table   myrecord
( id int  primary key  auto_increment,
  name varchar(200),
 city varchar(200)
);

(2)write code for insert.html  :
<form action=insert.php   method=post>
name <input type=text name=name>
city <input type=text name=city>
<input type="submit"  name="submit"  value="submit">
</form>

(3)now write code for  insert.php  file:

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

$name=$_POST['name'];
$city=$_POST['city'];

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

$sql = "INSERT INTO myrecord (name,city)
VALUES ('$name','$city')";

$result=mysqli_query($conn, $sql) or die(mysqli_error());
if($result)
{
echo "record  inserted";
}
else
{
echo "record not inserted";
}
mysqli_close($conn);

?>

Object  Oriented  way of  mysqli    code will be  just use given below code as "insert.php" file :

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$name=$_POST['name'];
$city=$_POST['city'];

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO myrecord (name,city) VALUES ('$name','$city')";

if ($conn->query($sql) === TRUE)
{
    echo "New record created successfully";

}
else

 {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();


?>



Now  example  for  update .html:

(1)write code for update.html:
<form action=update.php   method=post>
id <input type=text name=id>
name <input type=text name=name>
city <input type=text name=city>
<input type="submit"  name="submit"  value="submit">
</form>  

(2)write code for update.php :

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
 $id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];

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

$sql ="update myrecord set name='$name',city='$city' where id='$id'";


$result=mysqli_query($conn, $sql) or die(mysqli_error());
if($result)
{
echo "record  updated";
}
else
{
echo "record not updated";
}


mysqli_close($conn);

?>



Object  Oriented  way of  mysqli    code will be  just use given below code as update.php file :


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) 
{
    die("Connection failed: " . $conn->connect_error);

$sql = "update  myrecord set name='$name',city='$city'  where   id='$id'";

if ($conn->query($sql) === TRUE) 
{
    echo "New record updated successfully";
else

 {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();


?>


now  write code  for   search  :

(1)write code for  search.html :

<form action=search.php   method=post>
name <input type=text name=name>
<input type="submit"  name="submit"  value="submit">
</form> 
(2)write code for  search.php :

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$name=$_POST['name'];

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

$sql ="select  * from myrecord  where name='$name'";


$result=mysqli_query($conn, $sql) or die(mysqli_error());

if($result)
{
while($row=mysqli_fetch_array($result))
{

 echo $row['name'];
 echo $row['city'];
 }
}
else
{
 echo "record not found";
}


mysqli_close($conn);

?>


Object   Oriented    mysqli code for search   for search.php  file:

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

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

$sql = "SELECT * from myrecord where name='$name'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"];

     echo "-Name: " . $row["name"];

echo "-city: " . $row["city"]. "<br>";
    }
} else {
    echo " No results found";
}
$conn->close();
?>



Now  write code for  delete record :


(1)write code for delete.html  file:

<form action=delete.php   method=post>
id <input type=text name=id>

<input type="submit"  name="submit"  value="submit">
</form>  

(2)write  for delete.php file:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
 $id=$_POST['id'];


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

$sql ="delete from myrecord where id='$id'";


$result=mysqli_query($conn, $sql) or die(mysqli_error());
if($result)
{
 echo "record  deleted";
}
else
{
 echo "record not deleted";
}


mysqli_close($conn);

?>


Object   Oriented   mysqli    code  for   delete.php  file:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

$id=$_POST['id'];


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

$sql = "delete from myrecord  where  id='$id' ";

if ($conn->query($sql) === TRUE)
{
    echo "record deleted successfully";

}
else

 {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();


?>

========================================================================
Login  Code  examples using mysqli

(1)login.html    file  :

<form action="login1.php"  method="post">
username<input type=text name="username">
password <input type=text name="password">
<input type=submit name=submit value="login">
</form>

(2)now write code for connect.php file:

<?php
$con = mysqli_connect("localhost","root","","priyanka");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  else
  {
  echo "connected";
  }
?>



(3)now   write code for  login1.php  file:

<?php
include('connect.php');
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}

$username=$_POST['username'];
$password=$_POST['password'];
$sql = "select  username,password from  user where username='$username' and  password='$password'";

$result=mysqli_query($con, $sql) or die(mysqli_error());
$num=mysqli_num_rows($result);
if($num>0)
{

 echo "login successful";

}
else
{

 echo "else not successful";
}
mysqli_close($con);

?>



Browsers

Powered by Blogger.

Feature Post

About Me

My photo
B.E.(COMPUTER ENGINEER) PHP Web developer & Provide Training for Web Development , Digital Marketing ,Android Mobile Apps Development

Post Slider (Homepage)

Blog Archive

Malwares

Transfer Files

Media Players

Download Manager

Window

Torrents Soft

Developer Tools

Botton Ad

Ads

Newsletter

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top