Download presentation
Presentation is loading. Please wait.
Published byBenjamin Wilkerson Modified over 9 years ago
1
Module 3
2
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
3
Standard Query Language
4
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
5
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!
6
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
7
CREATE DATABASE database_name Eg. Create database friends;
8
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.
9
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type,.... )
10
CREATE TABLE Friends ( idnumber int, LastName varchar(255), FirstName varchar(255), Age varchar(255), Gender varchar(255) )
11
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
12
Id_numberLastnameFirstnameAgeGender 0001AgcopraJim21M 0002AgcopraJoseph26M 0003GalamitonPalquincy21M 0004BelonoCyril22F 0005GenerKathleen30F 0006De CastroRose Ann23F 0007CastilloJoan Rose28F 0008LlanderalJoan Rey29F 0009CarbonellAndrew27M 0010RoaBambi Rey28M 0011AgcopraJason28M
13
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
14
and SELECT * FROM table_name WHERE [Conditions] Eg. Select * From ekek where lastname = ‘Gargar’;
15
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’
16
Standard Query Language
17
Know your HTMLS Know common PHP Commands and Structures Master your SQL
18
1. Check if there is an PHP-SQL connection 2. Use a Database 3. Get a Table and Execute Queries 4. Extract Data from the Queries. 5. Close your Database after use.
19
<?php $connection = mysql_connect(‘localhost’,’root’,’password’) or die (‘Unable to Connect’); if($connection!=NULL) { echo "SQL is Connected to PHP"; }
21
mysql_select_db('friends') or die ('Unable to select a database!');
22
$query = 'Select * FROM names'; $result = mysql_query($query) or die (‘error in query’); ('Error in query: $query. '. msql_error());
23
There are 3 different ways to extract data: Mysql_fetch_row() Mysql_fetch_assoc() Mysql_fetch_object()
24
1] echo " "; 2] 3] if(mysql_num_rows($result) > 0) 4] { 5]while($row = mysql_fetch_row($result)) 6]{ 7]echo " $row[1], $row[2] "; 8]} 9]} 10]echo " ";
25
1] echo " "; 2] 3] if(mysql_num_rows($result) > 0) 4] { 5]while($row = mysql_fetch_assoc($result)) 6]{ 7] echo " $row[‘lastname’], $row[‘firstname’] "; 8]} 9]} 10]echo " ";
26
1] echo " "; 2] 3] if(mysql_num_rows($result) > 0) 4] { 5]while($row = mysql_fetch_object($result)) 6]{ 7] echo " $row->lastname, $row->firstname "; 8]} 9]} 10]echo " ";
27
mysql_free_result($result); mysql_close($connection); ?>
28
<?php $connection = mysql_connect(‘localhost’,’root’,’password’); mysql_select_db('friends') ; $query = 'Select * FROM names'; $result = mysql_query($query); echo " "; if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_row($result)) { echo " $row[1], $row[2] "; } echo " "; mysql_free_result($result); mysql_close($connection); ?>
29
PHP Commands mysql_connect(a,b,c ) a = dbserver b = user c = 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()
30
1 st Activity for the Semi-Finals
31
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
32
1 st Page: The Site will Ask for the Lastname 1 st Page: The Site will Ask for the Lastname 2 nd Page: The Site will Show the Record 2 nd Page: The Site will Show the Record Does the record exist? Error Page: The Site give a feedback Error Page: The Site give a feedback False True
33
Show your work on next Tuesday.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.