PHP with MySQL - LEARNING PHP AND MYSQL (2015)

LEARNING PHP AND MYSQL (2015)

PHP with MySQL

PHP connected to MySQL for data manipulation. It also provides cross platform functionality.

pHP-with-MySQL.png

PHP with MySQL

In a web server it there is a PHP interpreter which get an input from the PHP file space. This input is nothing

PHP creates a MySQL. It can be created by using–

· MySQLi- My SQL improvement.

· PDO- PHP data objects.

Example

MySQLi

<? Php

$servername = "local host";

$username = "username";

$password = "password";

// Create connection

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

// Check connection

if ($conn->connect_error)

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

// Create database

$sql = "CREATE DATABASE myDB";

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

{
echo "Database created successfully";
}

else

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

$conn->close();

?>

PDO

<? php

$servername = "local host";

$username = "username";

$password = "password";

try

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

// set the PDO error mode to exception

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

$sql = "CREATE DATABASE myDBPDO";

// use exec() because no result returned

$conn->exec($sql);

echo "Database created <br>";

}

catch(PDOException $e)

{

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

}

$conn = null;

?>

Syntax rules

· Query has to be quoted in PHP.

· Numeric value must be avoided.

· NULL value should not be quoted, it should be avoided.

High efficient statements

· Preparation statement- Is an SQL statement use to create and send a template to the particular database.

· Execute statement- Is used to bind all the retrieved values and to execute them.