DB Create and Insert

时间:2023-03-08 15:58:54
DB Create and Insert

<?php

  $servername = "localhost";

  $username = "username";

  $password = "password";

  

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

  if($conn -> connect_error){

    die("Connectionn failed:" . $conn->connect_error);

  }

  $sql = "CREATE DATABASE myDB";

  if($conn -> query($sql) === TRUE){

    echo "Database created successfully";

  }else{

    echo "Error creating database:" . $conn->error;

  }

  $sql_table = "CREATE TABLE MyTable(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

          firstname VARCHAR(30) NOT NULL,

          lastname VARCHAR(30) NOT NULL,

          email VARCHAR(50),

          reg_date TIMESTAMP

          )";

  if($conn->query($sql) === TRUE){

    echo "Table MyTable created successfully";

  }else{

    echo "Error creating table:" . $conn->error;

  }

  $sql_record = "INSERT INTO MyTable(firstname, lastname, email) VALUES ('J', 'Lam', 'x@xx.com')";

  if($conn -> query($sql) === TRUE){

    $last_id = $conn -> insert_id;

    echo "New record created successfully";

  }else{

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

  }

  $sql_multiInsert = "INSERT INTO MyTable(firstname, lastname, email) VALUES ('Jo', 'Do', 'Jo@xx.com')";

  $sql_multiInsert .= "INSERT INTO MyTable(firstname, lastname, email) VALUES('Ma', 'Mo', 'Ma@xx.com')";

  $sql_multiInsert .= "INSERT INTO MyTable(firstname, lastname, email) VALUES('Ju', 'Do', 'Ju@xx.com')";

  if($conn -> multi_query($sql_multiInser) === True){

    echo "New records created successfully";

  }else{

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

  }

  $conn ->  close();

?>

<?php

  $servername = "servername";

  $username = "username";

  $password = "password";

  $conn = mysqli_connect($servername, $username, $password);

  if($conn){

    die("Connection failed:" . mysqli_connect_error());

  }

  $sql = "CREATE DATABASE myDB";

  if(mysqli_query($conn, $sql)){

    $last_id = mysqli_insert_id($conn);

    echo "Database created successfully";

  }else{

    echo "Error creating database:" . mysqli_error($conn);

  }

  $sql_table = "CREATE TABLE MyTable(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

          firstname VARCHAR(30) NOT NULL,

          lastname VARCHAR(30) NOT NULL,

          email VARCHAR(50),

          reg_date TIMESTAMP

          )";

  if(mysqli_query($conn, $sql)){

    echo "Table MyTable created successfully";

  }else{  

    echo "Error creating table:" . mysqli_error($conn);

  }

  

  $sql_record = "INSERT INTO MyTable(firstname, lastname, email) VALUES ('J', 'Lam', 'x@xx.com')";

  if(mysqli_query($conn, $sql)){

    $last_id = $conn -> lastInsertId();

    echo "New record created successfully";

  }else{

    echo "Error:" . $sql . "<br>" . mysqli_error($conn);

  }

  

  $sql_multiInsert = "INSERT INTO MyTable(firstname, lastname, email) VALUES ('Jo', 'Do', 'Jo@xx.com')";

  $sql_multiInsert .= "INSERT INTO MyTable(firstname, lastname, email) VALUES('Ma', 'Mo', 'Ma@xx.com')";

  $sql_multiInsert .= "INSERT INTO MyTable(firstname, lastname, email) VALUES('Ju', 'Do', 'Ju@xx.com')";

  if(mysqli_multi_query($conn, $sql)){

    echo "New records created successfully";

  }else{

    echo "Error:" .$sql . "<br>" . mysqli_error($conn);

  }

  

  mysqli_close($conn);

?>

<?php

  $servername = "localhost";

  $username = "username";

  $password = "password"

  try{

    $conn = new PDO("mysql::host=$servername;dbname=myDB", $username, $password);

    $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "CREATE DATABASE myDB";

    $conn -> exex($sql);

    echo "Database created successfully<br>";

    $sql_table = "CREATE TABLE MyTable(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

          firstname VARCHAR(30) NOT NULL,

          lastname VARCHAR(30) NOT NULL,

          email VARCHAR(50),

          reg_date TIMESTAMP

          )";

    $conn -> exec($sql);

    echo "Table MyTable created successfully";

    $sql_record = "INSERT INTO MyTable(firstname, lastname, email) VALUES ('J', 'Lam', 'x@xx.com')";

    $conn -> exec($sql);

    echo "New record created successfully";

    $conn -> beginTransaction();

    $conn -> exec(INSERT INTO MyTable(firstname, lastname, email) VALUES ('Jo', 'Do', 'Jo@xx.com'));

    $conn -> exec(INSERT INTO MyTable(firstname, lastname, email) VALUES('Ma', 'Mo', 'Ma@xx.com'));

    $conn -> commit();

    echo "New records created successfully";

  }catch(PDOException $e){

    echo $sql. "<br>" . $e -> getMessage();

  }

?>