0% found this document useful (0 votes)
23 views

Unit 4 WP Notes

Uploaded by

Raj Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Unit 4 WP Notes

Uploaded by

Raj Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

What is MySQL?

• MySQL is a database system used on the web


• MySQL is a database system that runs on a
server

•MySQL is ideal for both small and large


applications

MySQL is very fast, reliable, and easy to use

MysQL uses standard SQL


• MySQL compiles on a number of platforms
•MySQL is free to download and use
• MySQL is developed, distributed, and

supported by Oracle Corporation


•MySQL is named after co-founder Monty
Widenius's daughter: My

The data in a MySQL database are stored in tables.

A table is a collection of related data, and it consists


of columns and rows.
PHP 5 and later can work with a MySQL database
using:

• MysQLi extension (the"i" stands for


improved)
•PDO (PHP Data objects)

Earlier versions of PHP used the MySQL


extension. However, this extension was
deprecated in 2012.

Should | Use MySQLior PD0?


If youneed a short answer, it would be "Whatever

youlike".

Both MySQLi and PDOhave their advantages:

PDOwill work on 12 different database systems,


whereas MySQLi will only work with MySQL
databases.
MySQL Examples in Both
MySQLi and PDO Syntax
In this, and in the following chapters we demonstrate
three ways of working with PHP and MySQL:

•MySQLi (object-oriented)
• MySQLi (procedural)
•PDO

MySQLi Installation

For Linux and Windows: The MySQLi extension is

automatically installed in most cases, when php5


mysql package is installed.

For installation details, go to:


http://php.net/manual/en/mysqli.installation. php

PDO Installation

For installation details, go to:


http://php.net/manual/en/pdo.installation. php
Open a Connection to MySQL
Before we can access data in the MySQL database,
we need tobe able to connect to the server:

Example (MySQLi Object


Oriented) Get your own PHP Server

<?php
$servername = "localhost" ;
$username = "username";
$password = "password";

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

// Check connection
if ($conn->connect_error) {
die("Connection failed: "
$conn-> connect_error) ;
}
echo "Connected successfully'";
?>
Example (MySQLiProcedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password" ;

Create connection
/

$conn= mysqli_connect ($servername,


$username, $password);

// Check connection
if (!$conn) {
die("Connection failed: "
mysqli_connect_error ());
}
echo "Connected successfully";
?>
Example (PDO)

<?php
$servername = "localhost" ;
$username = "username";
$password = "password" ;
try {
$Conn = new
PDO ("mysql :host-$servername; dbname=
myDB", $username, $password);
1/ set the PDO error mode to

exception
$conn
>setAttribute (PD0 ::ATTR_ERRMODE,
:
PDO: ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " $e

>getMessage () ;
?>
Close the Connection

The connection will be closed automatically when


the script ends.Toclose the connection before, use
the following:

MySQLiObject-Oriented:

$conn->close () ;

MySQLi Procedural:

mysqli_close ($conn) ;

PDO:

$conn = null;
Create a MySQL Database
Using MysQLi and PDO
The CREATE DATABASE statement is used to create

adatabase in MySQL.

The following examples create a database named


"myDB":
Example (MySQLi Object

oriented) Get your own PHP Server

<?php
$servername = "localhost" ;
$username = "username"
$password = "password";

1/ Create connection
$conn = new mysqli ($servername,
$username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: "
$conn->connect_error);
}

1/ Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created
successfully";
} else {
echo "Error creating database:
$conn->error;
}

$conn->close ();
?>
Example (MySQLiProcedural)

<?php
$servername = "localhost'" ;
$username = "username";
$password = "password";

// Create connection
$conn= mysqli_connect ($servername,
$username, $password);
// Check connecti on
if (!$conn) {
die("Connecti on failed:
mysqli_connect_error (O);
}

// Create database
$sql = "CREATE DATABASE myDB";
if (mysqli_query ($conn, $sql) ) {
echo "Database created
successfully '";
}else {
echo "Error creating database: "
mysqli_error($conn);

mysqli_close ($conn) ;
?>
Example (PDO)

<?php
$servername = "localhost'";
$username = "username";
$password = "password";

try {
$conn = neW
PDO ("mysql : host=$servername"
$username, $password);
1/ set the PDO error mode to
exception
$conn
>setAttribute(PDO::ATTR_ERRMODE,
PDO: :ERRMODE_EXCEPTION);
$sql "CREATE DATABASE myDBP DO";
// use exec() because no results
are returned
$conn->exec ($sql) ;
"
echo Database created
successfully<br>";
} catch (PDOException $e) {
echo $sql "<br>". $e

>getMessage () ;
}
$conn = null;
?>
Create aMySQL Table Using
MySQLi and PDO
The CREATE TABLE statement is used to create a
table in MySQL.

We will create a table named "MyGuests", with five

columns: "id", "firstname", "lastname","email"and

"reg_date":

CREATE TABLE MyGuests (


id INT (6) UNSIGNED AUTO_ INCREMENT
PRIMARY KEY,
firstname VARCHAR (30) NOT NULL,
lastname VARCHAR (30) NOT NULL,
email VARCHAR (50),
reg_date TIMESTAMP DEFAULT
CURRENT_TIMESTAMP ON UPDATE
CURRENT TIMESTAMP
)
Insert MySQL Using
Data Into
MySQLi and PDO

After a database and a table have been created, 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
quoted
• Numeric values must not be quoted

• The word NULL must not be quoted

The INSERT INT0 statement is used to add new


records to a MySQL table:

INSERT INTO table_name (column1,


column2, column3, . ..)
VALUES (valuel, value2, value3,...)

To learn more about SQL, please visit our SQL


tutorial.

In the previous chapterwe created an empty table


named "MyGuests" with five columns: "id",
"email" and "reg_date". Now,
"firstname","lastname",

let us fillthe table with data.


Select Data From a MySQL
Database

The SELECT statement is used to select data from


one or more tables:

SELECT column_name (s) FROM


table_name

orwe can use the * character to select ALL columns


from a table:

SELECT * FROM table_name

To learn more about SQL, please visit our SQL


tutorial.

You might also like