Chapter 4-Connecting To Databases

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Chapter 4- connecting to databases

Outline
4.1. Connect to an existing Database
4.2. Send Data to a Database
4.3. Retrieve Data from a Database
4.4. Modify Existing Data
4.5. Remove Existing Data
4.6. Data base security using server side scripting
Unit 4 Connecting to Databases
• What is MySQL?
• MySQL is the most popular open-source database system. MySQL is a
database.
• The data in MySQL is stored in database objects called tables.
• A table is a collections of related data entries and it consists of
columns and rows. Databases are useful when storing information
categorically. A company may have a database with the following
tables: "Employees", "Products", "Customers" and "Orders".
Connecting to an existing Database
Before we can access data in the MySQL database, we need to be able to connect to the server:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname=“name of database”;

// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);

// Check connection
if ($conn) {
   echo "Connected successfully";  
}
else{ echo “not connected”;}
?>
…Cont…
• The connection will be closed automatically when the script ends. To
close the connection before, use the following:

mysqli_close($conn);

Sending Data to Database;

• 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
The word NULL must not be quoted
• The INSERT INTO statement is used to add new records to a MySQL
table:
$query=“INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)”;
Retrieving data from database
• 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
• or we can use the * character to select ALL columns from a table:
SELECT * FROM table_name
Modify existing Data
• The UPDATE statement is used to update existing records in a table:
• UPDATE table_name SET column1=value, column2=value2,...
WHERE some_column=some_value 
• Note the where close specifies which record/s should be updated.
Removing existing Data
• The DELETE statement is used to delete records from a table:
• DELETE FROM table_name
WHERE some_column = some_value
example
$sql = "DELETE FROM MyGuests WHERE id=3";

if (mysqli_query($conn, $sql)) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}
Database Security Using server Side
Scripting

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy