Giter VIP home page Giter VIP logo

phpcrud's Introduction

CRUD in PHP MYSQL

Here is the basic tutorial how to do crud in mysqli and pdo.

  • first create a database name crud.
CREATE DATABASE crud;
  • select crud database.
USE crud;
  • Create a people table with name and email field
CREATE TABLE people (
    id INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL
);

CRUD using Mysqli

  • Connect php with mysql through mysqli_connect function
$host = 'localhost';
$dbname = 'crud';
$user = 'root';
$password = '';

$conn = mysqli_connect($host, $user, $password, $dbname);
  • Check connection successful or not
if ($conn->connect_errno) {
  echo 'not connected';
}else {
  echo 'connect successfully';  
}
  • Insert a row in database (C)
 $conn->query( "INSERT INTO people(name, email) VALUES ('polo', '[email protected]')" ) 
  • Read from database (R)
$results = $conn->query('SELECT * FROM people');
while($row = $results->fetch_assoc()) {
  echo $row['name'] . " " . $row['email'] . '<br/>';
}
  • Update Database (U)
 $conn->query( "UPDATE PEOPLE SET name='vasanth', email='[email protected]' WHERE id=3" ) 
  • Delete Database (D)
 $conn->query( "DELETE FROM people WHERE id=3" ) 

CRUD using PDO

CRUD using PDO actully best practice for php crud. This is because PDO is database agnostic. I mean you can use any database whether its mysql, postgresql or oracle.

  • Connect php with mysql through pdo instantiation and check whether pdo connection successful or not using by writing pdo instantiation inside try catch block;
$server = 'localhost';
$user = 'root';
$password = '';
$dbname = 'crud';
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_PERSISTENT => false,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
];

//connection 

try {
  $conn = new PDO("mysql:dbname=$dbname;host=$server", $user, $password, $options);
  echo 'connection successful';
} catch (PDOExceptions $e) {
  echo $e->getMessage();
}

here PDO took 4 arguments. dsn, username, password, options. dsn means data souce name. in our case dsn is

"mysql:dbname=$dbname;host=$server"

In dsn first we mention which driver we will use. In our case we use mysql. then put a : and give dbname and host name.

  • Insert a row in database using direct sql query (C)
$conn->query("INSERT INTO people (name, email) VALUES('polo', '[email protected]')");
  • Insert a row using prepare method in database (C)
$name = 'polo';
$email  = '[email protected]';
$statement = $conn->prepare("INSERT INTO people (name, email) VALUES(:name, :email)");
$statement->execute([':name' => $name, ':email' => $email]);

So from state above we understand we can insert data into database using direct sql query or prepare statement. We always use prepare statement. Since its more flexible when we work with variable data. In real work we will insert data with variable data. Beside this we can sanitize data when execute function call.

  • Read from database (R)
$statement = $conn->prepare( "SELECT * FROM people" );
$statement->execute();
while($row = $statement->fetch()) {
  echo '<b>Name:</b>: ' . $row->name . " <b>Email:</b> " . $row->email . '<br/>';
  echo '<br/>';
}
  • Update Database (U)
$name = 'vasanth';
$email  = '[email protected]';
$id = 1;
$statement = $conn->prepare("UPDATE people SET name=:name, email=:email WHERE id=:id");
$statement->execute([':name' => $name, ':email' => $email, ':id' => $id]);
  • Delete Database (D)
$statement = $conn->prepare("DELETE FROM people WHERE id=:id");
$statement->execute([':id'=> 28]);

Thats all. Hope Now you will be able to do crud operation using mysqli or PDO in php.

phpcrud's People

Contributors

polodev avatar

Watchers

James Cloos avatar sxhylkl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.