0% found this document useful (0 votes)
46 views6 pages

Unit - V PHP (DS & Stat)

The document outlines the syllabus for interacting with MySQL using PHP, highlighting the differences between MySQL and MySQLi functions, and providing instructions for connecting to MySQL and executing queries. It covers creating an online address book, including database table creation, record addition, viewing, and deletion mechanisms. Additionally, it includes examples of inserting and retrieving data using PHP with MySQLi syntax and functions.

Uploaded by

Divya Gurram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views6 pages

Unit - V PHP (DS & Stat)

The document outlines the syllabus for interacting with MySQL using PHP, highlighting the differences between MySQL and MySQLi functions, and providing instructions for connecting to MySQL and executing queries. It covers creating an online address book, including database table creation, record addition, viewing, and deletion mechanisms. Additionally, it includes examples of inserting and retrieving data using PHP with MySQLi syntax and functions.

Uploaded by

Divya Gurram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT – V

SYLLABUS
Interacting with MYSQL using PHP: MYSQL Versus MYSQLI Functions, Connecting to
MYSQL with PHP, Working with MYSQL Data.

Creating an Online Address Book: Planning and Creating Database Tables, Creating
Menu, Creating Record Addition Mechanism, Viewing Records, Creating the Record
Deletion Mechanism, Adding Sub – entities to a Record.

…………………………………………………………………………………………………..

 MYSQL versus MYSQLI Functions:


MYSQL and MYSQLi are PHP database extensions implemented by using PHP extension
framework. PHP database extensions are used to write PHP code for accessing the database.
They expose database API to provide interfaces to use database functions.

MYSQL extension is deprecated and will not be available in future PHP versions. It is
recommended to use the MYSQLi extension with PHP 5.5 and above.

MYSQL, MYSQLi Difference

There are too many differences between these PHP database extensions. These differences
are based on some factors like performance, library functions, features, benefits, and others.

MYSQL MYSQLi

MYSQL extension added in PHP version MYSQLi extension added in PHP 5.5 and
2.0. and deprecated as of PHP 5.5.0. will work on MYSQL 4.1.3 or above.
Does not support prepared statements. MYSQLi supports prepared statements.
MYSQL provides the procedural interface. MYSQLi provides both procedural and
object – oriented interface.
MYSQL extension does not support stored MYSQLi supports store procedure.
procedure.
MYSQL extension lags in security and other MYSQLi extension is with enhanced
special features, comparatively. security and improved debugging.
Transactions are handled by SQL queries MYSQLi supports transactions through
only. API.
Extension directory: ext/mysql. Extension directory: ext/mysqli.

Other MYSQLi Advantages

 MYSQLi function mysqli_query() allows to enforce error prone queries and prevents
bugs like SQL injection.
 Using MYSQLi data fetch, we can get buffered or unbuffered based on server
resource size.
 MYSQLi API allows executing multiple queries with single expression using
multi_query() function.

Connecting to MYSQL with PHP:


Before we can access data in the MYSQL database, we need to be able to connect to the
server. The basic syntax for a connection to MYSQL is as follows:

Syntax: $mysqli = mysqli_connect(“hostname”, “username”, “password”, “database”);

Close the connection

The connection will be closed automatically when the script ends. To close the connection
before , use the following: $conn->close();

Executing Queries

<?php

$servername = “localhost”;

$username = “root”;

$password = “ ”;

$dbname = “myDB”;

//create connection

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

//check connection

if ($conn->connect_error) {

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

// sql to create table

$sql = "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

)";

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

echo "Table MyGuests created successfully";

} else {

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

$conn->close();

?>

Working with MYSQL Data:


a.) Inserting Data with PHP
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 INTO statement is used to add new records to a MYSQL table:

Syntax:

INSERT INTO table_name (column1, column2, …….)VALUES (value1, value2,……);

Example:

<?php

$servername= "localhost";
$username= "username";
$password= "password";
$dbname = "myDB";
// Create connection

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

// Check connection

if ($conn->connect_error) {

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

$sql = "INSERT INTO MyGuests (firstname, lastname, email)

VALUES ('John', 'Doe', '[email protected]')";

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

echo "New record created successfully";

} else {

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

$conn->close();
?>

b.) Retrieving Data with PHP:


The SELECT statement is used to select data from one or more tables:

Syntax:

SELECT column_name(s) FROM table_name;

Or we can use the * character to select ALL columns from a table:

Syntax:

SELECT * FROM table_name;


Example:

<?php

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";

// Create connection

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

// Check connection

if ($conn->connect_error) {

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

$sql = "SELECT id, firstname, lastname FROM MyGuests";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// output data of each row

while($row = $result->fetch_assoc()) {

echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

} else {

echo "0 results";

$conn->close();

?>

Output:

id: 1 - Name: John Doe

id: 2 - Name: Mary Moe


id: 3 - Name: Julie Dooley

You might also like