module 3. 1. review basic sql commands: create database, create table, insert and select 2. connect...
TRANSCRIPT
![Page 1: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/1.jpg)
Module 3
![Page 2: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/2.jpg)
1. Review Basic SQL commands: Create Database, Create Table, Insert and Select
2. Connect an SQL Database to PHP
3. Execute SQL Commands in PHP
![Page 3: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/3.jpg)
Standard Query Language
![Page 4: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/4.jpg)
SQL stands for Structured Query Language SQL is a standard language for accessing
databases. MySQL, SQL Server, Access, Oracle,
Sybase, DB2, and other database systems. SQL lets you access and manipulate
databases SQL is an ANSI (American National
Standards Institute) standard
![Page 5: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/5.jpg)
Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!
![Page 6: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/6.jpg)
To build a web site that shows some data from a database, you will need the following:• An RDBMS database program (i.e. MS
Access, SQL Server, MySQL)• A server-side scripting language, like PHP or
ASP• SQL• HTML / CSS
![Page 7: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/7.jpg)
CREATE DATABASE database_name•Eg. Create database friends;
![Page 8: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/8.jpg)
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
![Page 9: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/9.jpg)
CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,column_name3 data_type,....)
![Page 10: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/10.jpg)
CREATE TABLE Friends(idnumber int,LastName varchar(255),FirstName varchar(255),Age varchar(255),Gender varchar(255))
![Page 11: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/11.jpg)
Constraints are used to limit the type of data that can go into a table.
Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement).• NOT NULL• UNIQUE• PRIMARY KEY• FOREIGN KEY• CHECK• DEFAULT
![Page 12: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/12.jpg)
Id_number Lastname Firstname Age Gender
0001 Agcopra Jim 21 M
0002 Agcopra Joseph 26 M
0003 Galamiton Palquincy 21 M
0004 Belono Cyril 22 F
0005 Gener Kathleen 30 F
0006 De Castro Rose Ann 23 F
0007 Castillo Joan Rose 28 F
0008 Llanderal Joan Rey 29 F
0009 Carbonell Andrew 27 M
0010 Roa Bambi Rey 28 M
0011 Agcopra Jason 28 M
![Page 13: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/13.jpg)
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax•SELECT column_name(s)FROM table_name
and•SELECT * FROM table_name
![Page 14: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/14.jpg)
and•SELECT * FROM table_name WHERE [Conditions]
•Eg. Select * From ekek where lastname = ‘Gargar’;
![Page 15: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/15.jpg)
SELECT * FROM NAMES SELECT Lastname,Firstname From
Names SELECT * FROM Names Where
Lastname = ‘Agcopra’ SELECT * FROM Names Where
Firstname like ‘A%’; Select * From Names Where Age > 25
AND Gender = ‘M’
![Page 16: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/16.jpg)
Standard Query Language
![Page 17: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/17.jpg)
Know your HTMLS Know common PHP Commands and Structures
Master your SQL
![Page 18: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/18.jpg)
1. Check if there is an PHP-SQL connection
2. Use a Database3. Get a Table and Execute Queries4. Extract Data from the Queries.5. Close your Database after use.
![Page 19: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/19.jpg)
<?php
$connection = mysql_connect(‘localhost’,’root’,’password’) or die (‘Unable to Connect’);
if($connection!=NULL){echo "SQL is Connected to PHP";}
![Page 20: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/20.jpg)
![Page 21: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/21.jpg)
mysql_select_db('friends') or die ('Unable to select a database!');
![Page 22: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/22.jpg)
$query = 'Select * FROM names';
$result = mysql_query($query) or die (‘error in query’);
('Error in query: $query. ' . msql_error());
![Page 23: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/23.jpg)
There are 3 different ways to extract data:• Mysql_fetch_row()• Mysql_fetch_assoc()• Mysql_fetch_object()
![Page 24: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/24.jpg)
1] echo "<ol>";2] 3] if(mysql_num_rows($result) > 0)4] {5] while($row =
mysql_fetch_row($result))6] {7] echo "<li> <b>$row[1]</b>,
$row[2] </li>";8] }9] }10]echo "</ol>";
![Page 25: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/25.jpg)
1] echo "<ol>";2] 3] if(mysql_num_rows($result) > 0)4] {5] while($row =
mysql_fetch_assoc($result))6] {7] echo "<li> <b>$row[‘lastname’]</b>,
$row[‘firstname’] </li>";8] }9] }10]echo "</ol>";
![Page 26: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/26.jpg)
1] echo "<ol>";2] 3] if(mysql_num_rows($result) > 0)4] {5] while($row =
mysql_fetch_object($result))6] {7] echo "<li> <b>$row->lastname</b>, $row-
>firstname </li>";
8] }9] }10]echo "</ol>";
![Page 27: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/27.jpg)
mysql_free_result($result);mysql_close($connection);
?>
![Page 28: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/28.jpg)
<?php
$connection = mysql_connect(‘localhost’,’root’,’password’);mysql_select_db('friends') ;$query = 'Select * FROM names';$result = mysql_query($query);echo "<ol>";if(mysql_num_rows($result) > 0)
{while($row = mysql_fetch_row($result))
{echo "<li> <b>$row[1]</b>, $row[2] </li>";}
}echo "</ol>";mysql_free_result($result);mysql_close($connection);
?>
![Page 29: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/29.jpg)
PHP Commands
mysql_connect(a,b,c )a = dbserverb = userc = password
mysql_select_db(a)a = database
mysql_query(a)a = SQL command
mysql_num_rows()Counts the number of rows
$a=mysql_fetch_row(a)
$a=mysql_fetch_assoc()
$a=mysql_fetch_object()
mysql_free_result(a) Mysql_close()
![Page 30: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/30.jpg)
1st Activity for the Semi-Finals
![Page 31: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/31.jpg)
1. Make a Database named DB_31_[your block]
2. Make a Table named Employees• Data Entity and Attributes:• Idnum• Lastname• Firstname• Department (Admin, Logistics, Sales,
Accounting)
• Years• Gender
![Page 32: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/32.jpg)
1st Page:
The Site will Ask for the Lastname
2nd Page:
The Site will Show the Record
Does the
record
exist?
Error Page:
The Site give a feedback
False
True
![Page 33: Module 3. 1. Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in](https://reader035.vdocuments.net/reader035/viewer/2022062308/56649f565503460f94c79863/html5/thumbnails/33.jpg)
Show your work on next Tuesday.