MySQLi and PDO

Posted on Updated on

UntitledghfhfghfhMySQLi and PDO

How we can Insert Data Into MySQL With help of  MySQLi and PDO

First of all we have to create a database and then create table into that database, now we can start adding data in them.

Here are some syntax rules to follow:

  • The SQL query must be quoted in PHP
  • String values inside the SQL query must be in quote
  • Numeric values must not be quoted
  • The word NULL must not be quoted

The INSERT Query is used to insert new records to a MySQL table:

INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

This example add new record to the “MyGuests” table:

Example (MySQLi Object-oriented)

<?php
$server_name = “localhost”;
$user_name = “username”;
$user_password = “password”;
$db_name = “myDB”;

// Create connection
$conn = new mysqli($server_name, $user_name, $user_password, $db_name);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

$sql = “INSERT INTO MyGuests (firstname, lastname, email)
VALUES (‘John’, ‘Doe’, ‘asse@example.com’)”;

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

$conn->close();
?>

Example (MySQLi Procedural)

<?php
$server_name = “localhost”;
$user_name = “username”;
$user_password = “password”;
$db_name = “myDB”;

// Create connection
$conn = mysqli_connect($server_name, $user_name, $user_password, $db_name);
// Check connection
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}

$sql = “INSERT INTO MyGuests (first_name, last_name, e_mail)
VALUES (‘John’, ‘Doe’, ‘techno@example.com’)”;

if (mysqli_query($conn, $sql)) {
echo ” insert successfully”;
} else {
echo “Error: ” . $sql . “<br>” . mysqli_error($conn);
}

mysqli_close($conn);
?>

Example (PDO)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDBPDO”;

try {
$conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = “INSERT INTO MyGuests (firstname, lastname, email)
VALUES (‘John’, ‘Doe’, ‘campus@example.com’)”;

$conn->exec($sql);
echo “insert successfully”;
}
catch(PDOException $e)
{
echo $sql . “<br>” . $e->getMessage();
}

$conn = null;
?>

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s