CGS 2835 Interdisciplinary Web Development
SQL – The Basics
CGS 2835 Interdisciplinary Web Development
Database Strengths
• Data can be sifted, sorted
and queried through the
use of ...
CGS 2835 Interdisciplinary Web Development
Data Manipulation Language
• A Data Manipulation Language (DML) is a specific
l...
CGS 2835 Interdisciplinary Web Development
SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a ...
CGS 2835 Interdisciplinary Web Development
SELECT
SELECT field_names(s)
FROM table_name
Examples:
SELECT LastName,FirstNam...
CGS 2835 Interdisciplinary Web Development
SELECT
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
E...
CGS 2835 Interdisciplinary Web Development
SELECT
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
A...
CGS 2835 Interdisciplinary Web Development
UPDATE
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_colum...
CGS 2835 Interdisciplinary Web Development
INSERT INTO
INSERT INTO table_name
(ColumnName1, … , ColumnNameN )
VALUES (‘dat...
CGS 2835 Interdisciplinary Web Development
DELETE
DELETE FROM table_name
WHERE some_column=some_value
Example
DELETE FROM ...
CGS 2835 Interdisciplinary Web Development
PHP > MySQL
CGS 2835 Interdisciplinary Web Development
Accessing a MySQL Database from PHP
First create a database, table, and fields ...
CGS 2835 Interdisciplinary Web Development
1. Establish a Connection
$mysqli = mysqli_connect($server, $mysql_username , $...
CGS 2835 Interdisciplinary Web Development
2. Get the $mysqli database variable
In PHP functions, we will refer to this va...
CGS 2835 Interdisciplinary Web Development
3. Use mysqli_query
to Issue Commands
global $mysqli;
mysqli_query($mysqli, "IN...
CGS 2835 Interdisciplinary Web Development
3. Use mysqli_query
to Issue Commands
global $mysqli;
$result = mysqli_query($m...
CGS 2835 Interdisciplinary Web Development
Useful Resources
• Tizag PHP/MySQL Tutorial
– http://www.tizag.com/mysqlTutoria...
Upcoming SlideShare
Loading in …5
×

Phpmysqlcoding

528 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
528
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Phpmysqlcoding

  1. 1. CGS 2835 Interdisciplinary Web Development SQL – The Basics
  2. 2. CGS 2835 Interdisciplinary Web Development Database Strengths • Data can be sifted, sorted and queried through the use of data manipulation languages.  The power of a database and DBMS lies in the user’s ability to manipulate the data to turn up useful information.
  3. 3. CGS 2835 Interdisciplinary Web Development Data Manipulation Language • A Data Manipulation Language (DML) is a specific language provided with the DBMS that allows people and other database users to access, modify, and make queries about data contained in the database, and to generate reports. • Structured Query Language (SQL): The most popular DML. – SELECT * FROM EMPLOYEE WHERE JOB_CLASSIFICATION = ‘C2”
  4. 4. CGS 2835 Interdisciplinary Web Development SQL Commands SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index From www.w3schools.com/sql
  5. 5. CGS 2835 Interdisciplinary Web Development SELECT SELECT field_names(s) FROM table_name Examples: SELECT LastName,FirstName FROM Employees SELECT * FROM Employees Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason LastName FirstName Baldauf Ola Svendson Jon Pettersen Kari Willis Carl Smith Jason Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason Employees Table
  6. 6. CGS 2835 Interdisciplinary Web Development SELECT SELECT column_name(s) FROM table_name WHERE column_name operator value Example SELECT * FROM Employees WHERE LastName=’Willis' Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason Employee_Id LastName FirstName Address City 4 Willis Carl 12 Bacon Cr Atlanta Employees Table
  7. 7. CGS 2835 Interdisciplinary Web Development SELECT SELECT column_name(s) FROM table_name WHERE column_name operator value AND/OR column_name operator value Example SELECT * FROM Employees WHERE LastName=’Willis’ OR LastName=‘Pettersen’ Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason Employee_Id LastName FirstName Address City 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta Employees Table
  8. 8. CGS 2835 Interdisciplinary Web Development UPDATE UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Example UPDATE Employees SET Address=’2727 Monroe St', City=’Tallahassee' WHERE LastName=’Smith' AND FirstName=’Jason' Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason Employees Table Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason 2727 Monroe St Tallahassee
  9. 9. CGS 2835 Interdisciplinary Web Development INSERT INTO INSERT INTO table_name (ColumnName1, … , ColumnNameN ) VALUES (‘data1’, … , ‘dataN’) Example INSERT INTO Employees (LastName, FirstName, Address, City) VALUES (‘Larkin’, ‘Robert’, ‘34 W 7th’, ‘Atlanta’) Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason Employees Table Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason 6 Larkin Robert 34 W 7th Atlanta
  10. 10. CGS 2835 Interdisciplinary Web Development DELETE DELETE FROM table_name WHERE some_column=some_value Example DELETE FROM Employees WHERE LastName=’Willis' AND FirstName=’Carl' Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 4 Willis Carl 12 Bacon Cr Atlanta 5 Smith Jason Employees Table Employee_Id LastName FirstName Address City 1 Baldauf Ola 120 Main St Chicago 2 Svendson Jon 3 Bogus Dr Tallahassee 3 Pettersen Kari 2413 Sayer Ave Tallahassee 5 Smith Jason 2727 Monroe St Tallahassee
  11. 11. CGS 2835 Interdisciplinary Web Development PHP > MySQL
  12. 12. CGS 2835 Interdisciplinary Web Development Accessing a MySQL Database from PHP First create a database, table, and fields using phpMyAdmin 1.Establish a connection to mySQL server 2.Get the $mysqli database variable 3.Use mysqli_query to issue SQL commands
  13. 13. CGS 2835 Interdisciplinary Web Development 1. Establish a Connection $mysqli = mysqli_connect($server, $mysql_username , $mysql_password, $database); $server = "localhost"; $mysql_username = "user"; $mysql_password = "pass"; $database = "test";
  14. 14. CGS 2835 Interdisciplinary Web Development 2. Get the $mysqli database variable In PHP functions, we will refer to this variable first with: global $mysqli; $mysqli = mysqli_connect($server, $mysql_username , $mysql_password, $database);
  15. 15. CGS 2835 Interdisciplinary Web Development 3. Use mysqli_query to Issue Commands global $mysqli; mysqli_query($mysqli, "INSERT INTO visitors (name, email) VALUES('Timmy Mellowman', 'mellowman@fsu.edu' ) ");
  16. 16. CGS 2835 Interdisciplinary Web Development 3. Use mysqli_query to Issue Commands global $mysqli; $result = mysqli_query($mysqli, "SELECT * FROM visitors”); while($row = mysql_fetch_array( $result )){ echo ”<p> Name: ".$row['name'] ."<br />"; echo "Email: ".$row['email'] ."<br />"; echo " Date: ".$row['date'] .”</p>"; }
  17. 17. CGS 2835 Interdisciplinary Web Development Useful Resources • Tizag PHP/MySQL Tutorial – http://www.tizag.com/mysqlTutorial • W3Schools – PHP MySQL: http://www.w3schools.com/php/php_mysql_intro.asp – SQL: http://www.w3schools.com/sql/default.asp • MySQL Manual: – http://dev.mysql.com/doc/refman/5.0/en • PHP MySQL functions: – http://www.php.net/manual/en/book.mysqli.php

×