SlideShare une entreprise Scribd logo
1  sur  27
SQL | PHP   Tutorial at 8am. god, it’s early.
SQL  intro ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Databases _ creation CREATE TABLE tableName ( name VARCHAR(55), sex CHAR(1), age INT(3), birthdate DATE, salary DECIMAL(10,2), primary key(name) ); Types of attributes:  char, varchar, int, smallint, decimal, date, float, etc. * varchar is a string with varying # of characters. In our example, 55 is the characters longest possible string allowed. * decimal(10,2) indicated 2 places after the decimal point and 10 total digits (including the decimal numbers)
Databases _ creation 2 CREATE TABLE tableName ( name VARCHAR(55), sex CHAR(1) NOT NULL, age INT(3), birthdate DATE, salary DECIMAL(10,2) DEFAULT ‘0.00’, primary key(name) ); Primary key:  primary key is a UNIQUE value. For every entry in your database this must be unique and not null and every DB must have one. NOT NULL:  column must have a value DEFAULT:  you can set a default value if no other value is inputted for that column.
Databases _ indexed primary keys Instead of specifying a column as a primary key you can have the database create a column of numbers that will automatically increment with each entry inserted into the DB. Example: CREATE TABLE tableName ( id INT AUTO_INCREMENT , name VARCHAR(55), sex CHAR(1), age INT(3), birthdate DATE, salary DECIMAL(10,2), primary key(id) ); Entry 1 will have 1 as a key. Entry 2 will have 2 and so forth.
Databases _ deletion DROP TABLE tableName;
Databases _ insertion Inserting data in the database: INSERT INTO tableName(name,sex,age) VALUES(‘Mr. Freeze’,’M’,42); Also valid: INSERT INTO tableName(sex,name,age) VALUES(‘F’,’Mr. Freeze’,42); Order doesn’t matter.
Databases _ the meat Always in the form of: SELECT …. FROM …. WHERE …. So  select  a column from your database. From  a database Where  x meets y condition. *  Except in the case of modification
Databases _ updating  Suppose we want to change Mr. Freeze’s age to 52. UPDATE tableName SET age = ’52’ WHERE name LIKE ‘Mr. Freeze’ And so forth.
Databases _ aggregates  This is the actual meat of using SQL. These are where you set your conditions, narrow down your table into a usable set. Here are the usable functions, I’ll show a quick example with each. The only way to really know this stuff is practice. Group by Count Sum Avg Min/Max Order by
Databases _ group by  This is the actual meat of using SQL. These are where you set your conditions, narrow down your table into a usable set. Here are the usable functions, I’ll show a quick example with each. The only way to really know this stuff is practice. Group by lumps all the common attributes into one row. SELECT employee_id, MAX(salary) FROM Works_In GROUP BY dept_id; * MAX selects the maximum value in its () likewise for MIN
Databases _ count  Count counts the number of columns with the specified attribute. SELECT term, COUNT(course_id) FROM teaches GROUP BY term; We counted the number of courses taught during x term. AVG & SUM function pretty much the same way.
 
PHP _ connecting to the db This is the basic connect script for accessing your db: <?php mysql_connect(“localhost”,”username”,”password”) or  die(mysql_error());  ?> Localhost indicates the current machine. So you’re asking the machine to connect to itself. The die(mysql_error) part says if there’s an error halt everything and display this error. If it errors on this part, it means either your host, username, or password are wrong.
PHP _ error checking w/ echo Consider the connection script again with this modification: <?php mysql_connect(“localhost”,”username”,”password”) or  die(mysql_error()); echo “Connected to database.” ?> Later on you  may be unable to differentiate where the error occurred. So while developing your code throw in some echo statements, they just print stuff to the screen. When PHP is done connecting to our database it tell us.
PHP _ select the database. <?php mysql_connect(“localhost”,”username”,”password”) or  die(mysql_error()); echo “Connected MySQL!”; mysql_select_db(“ljlayou_comp353” or die(mysql_error()); echo “Connected to database 353”; ?>
PHP _  create/drop table <?php mysql_connect(“localhost”,”username”,”pw”) or  die(mysql_error()); mysql_select_db(“ljlayou_comp353” or die(mysql_error()); mysql_query(“CREATE TABLE Works_In(…)“) or die(mysql_error()); ?> We’re querying PHP to tell MySQL to do something, in this case create the table. The same applies for dropping a table. As you can see our code is being reused over and over. It gets pretty repetitive like this. Again we tell php to stop everything if an error occurs.
PHP _  insertion <?php mysql_connect(“localhost”,”username”,”pw”) or  die(mysql_error()); mysql_select_db(“ljlayou_comp353” or die(mysql_error()); mysql_query(“INSERT INTO Works_In(company,position) VALUES(‘McDonalds’,’fry cook’)”); ?> We’re querying PHP to tell MySQL to do something, in this case create the table. The same applies for dropping a table. As you can see our code is being reused over and over. It gets pretty repetitive like this.
PHP _  selecting a table In order to manipulate, fetch, etc data from your database you must have PHP remember the result. So we store it in an array (?) to preserve “columns”. PHP variables unlike Java do not need a type declaration. From  now on I’ll be omitting the connect stuff. <?php […] $result = mysql_query(“SELECT * FROM Works_In”) or die(mysql_error()); $row = mysql_fetch_array($result); echo “company: “ .$row[‘company’]; echo “position:” .$row[‘position’]; ?> From these lines we see that each cell in the area is labeled under the column name. Using this method we can output or even compare data.
PHP _  selecting a table In order to manipulate, fetch, etc data from your database you must have PHP remember the result. So we store it in an array (?) to preserve “columns”. PHP variables unlike Java do not need a type declaration. From  now on I’ll be omitting the connect stuff. <?php […] $result = mysql_query(“SELECT * FROM Works_In”) or die(mysql_error()); $row = mysql_fetch_array($result); echo “company: “ .$row[‘company’]; echo “position:” .$row[‘position’]; ?> From these lines we see that each cell in the area is labeled under the column name. Using this method we can output or even compare data. The ‘*’ symbol in the SELECT statement just means that we select all the columns in the table. The above statement however results in the first row only being shown.
PHP _  selecting a table 2 To solve this problem, we loop continuously until there are no more rows to choose from. <?php […] while ($row = mysql_fetch_array($result)) { echo “company: “ .$row[‘company’].  “ | “position:” .$row[‘position’]; echo “<br/>”;} ?> If you have noticed the ‘.’ symbol signifies a concatenation.
PHP _  the formula We looked over it all. Here’s the general formula: <?php  mysql_connect(“ localhost ”,” username ”,” pw ”) or  die(mysql_error()); mysql_select_db(“ databaseName ” or die(mysql_error()); $result = mysql_query( yourQuery ) or die(mysql_error()); $row = mysql_fetch_array($result); while ($row = mysql_fetch_array($result)) {  …  }; ?> (Show 255 final)
 
PHP _  form processing Topic job but it’s all good. I’m assuming you know how to create forms in HTML. Else, well, google it. It’s  pretty straight forward. So let’s take this form as our  example, this is a snippet of the form code: <form name=“animas” action=“processform.php&quot; method=&quot;post”>   […] <b>FUN</b>:  <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;horns&quot;>Horns  <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;wings&quot;>Wings  <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;mane&quot;>Mane  <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;giraffe&quot;>Giraffe Neck <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;imagine it&quot; class=&quot;submit&quot; /> </form>
PHP _  form processing 2 Our form action tells the form what to do with the data. POST is a method that sends an array of variables, our data. Only when the submit button is pressed is the data sent. <form name=“animals” action=“processform.php&quot; method=&quot;post”>   […] <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;imagine it&quot; class=&quot;submit&quot; /> </form> It is common practice to create a separate file for form processing.
PHP _  form processing 3 Our data is now winding through the PHP tubes. Let’s look how it’s processed.  $_POST[‘submit’] if( $dbc = @mysql_connect(‘localhost’,’username’,’pw’)) { if(!@mysql_select_db(‘database’)) { die(mysql_error());} } else { die(mysql_error());} $query = “INSERT INTO animals(id,data,appendages, tail, sound,extra) VALUES(0,’{$_POST[‘body’]}’,’{$_POST[‘appendages’]}’, […] )” if(@mysql_query($query) { print “success”;} else { print “you lose”;} mysql_close();
PHP _  form processing 4 ,[object Object],[object Object],[object Object]

Contenu connexe

Tendances

Tendances (20)

basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Zend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample QuestionsZend Certification PHP 5 Sample Questions
Zend Certification PHP 5 Sample Questions
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Javascript
JavascriptJavascript
Javascript
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Practice exam php
Practice exam phpPractice exam php
Practice exam php
 
JavaScript 101 - Class 1
JavaScript 101 - Class 1JavaScript 101 - Class 1
JavaScript 101 - Class 1
 
Clean code
Clean codeClean code
Clean code
 
Learn php with PSK
Learn php with PSKLearn php with PSK
Learn php with PSK
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
PHP MySQL Workshop - facehook
PHP MySQL Workshop - facehookPHP MySQL Workshop - facehook
PHP MySQL Workshop - facehook
 
Javascript
JavascriptJavascript
Javascript
 
Php mysql
Php mysqlPhp mysql
Php mysql
 

En vedette

Pemrograman Web with PHP MySQL
Pemrograman Web with PHP MySQLPemrograman Web with PHP MySQL
Pemrograman Web with PHP MySQLdjokotingkir999
 
PHP MySQL database connections
PHP MySQL database connectionsPHP MySQL database connections
PHP MySQL database connectionsayman diab
 
Modul praktikum javascript
Modul praktikum javascriptModul praktikum javascript
Modul praktikum javascripthardyta
 
Php & My Sql
Php & My SqlPhp & My Sql
Php & My Sqlcecile59
 
PHP tutorial | ptutorial
PHP tutorial | ptutorialPHP tutorial | ptutorial
PHP tutorial | ptutorialPTutorial Web
 
Pemrograman web dengan php my sql
Pemrograman web dengan php my sqlPemrograman web dengan php my sql
Pemrograman web dengan php my sqlanarkonam
 
Perl programming language
Perl programming languagePerl programming language
Perl programming languageElie Obeid
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)Arjun Shanka
 
Tutorial php membuat Aplikasi Inventaris
Tutorial php membuat Aplikasi InventarisTutorial php membuat Aplikasi Inventaris
Tutorial php membuat Aplikasi InventarisDeka M Wildan
 
Examination Hall Allocation
Examination Hall Allocation Examination Hall Allocation
Examination Hall Allocation Martina Thampan
 
Ebook PHP - menyelam dan menaklukan samudra php
Ebook PHP - menyelam dan menaklukan samudra phpEbook PHP - menyelam dan menaklukan samudra php
Ebook PHP - menyelam dan menaklukan samudra phpPuguh Nugroho
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorialalexjones89
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
Tutorial Pembuatan Aplikasi Website Beserta Databasenya
Tutorial Pembuatan Aplikasi Website Beserta DatabasenyaTutorial Pembuatan Aplikasi Website Beserta Databasenya
Tutorial Pembuatan Aplikasi Website Beserta DatabasenyaRCH_98
 
Linux.ppt
Linux.ppt Linux.ppt
Linux.ppt onu9
 

En vedette (20)

Pemrograman Web with PHP MySQL
Pemrograman Web with PHP MySQLPemrograman Web with PHP MySQL
Pemrograman Web with PHP MySQL
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
PHP MySQL database connections
PHP MySQL database connectionsPHP MySQL database connections
PHP MySQL database connections
 
Modul praktikum javascript
Modul praktikum javascriptModul praktikum javascript
Modul praktikum javascript
 
Php & My Sql
Php & My SqlPhp & My Sql
Php & My Sql
 
Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
PHP tutorial | ptutorial
PHP tutorial | ptutorialPHP tutorial | ptutorial
PHP tutorial | ptutorial
 
Pemrograman web dengan php my sql
Pemrograman web dengan php my sqlPemrograman web dengan php my sql
Pemrograman web dengan php my sql
 
Php mysq l - siapa - takut
Php mysq l - siapa - takutPhp mysq l - siapa - takut
Php mysq l - siapa - takut
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Tutorial php membuat Aplikasi Inventaris
Tutorial php membuat Aplikasi InventarisTutorial php membuat Aplikasi Inventaris
Tutorial php membuat Aplikasi Inventaris
 
Examination Hall Allocation
Examination Hall Allocation Examination Hall Allocation
Examination Hall Allocation
 
Ebook PHP - menyelam dan menaklukan samudra php
Ebook PHP - menyelam dan menaklukan samudra phpEbook PHP - menyelam dan menaklukan samudra php
Ebook PHP - menyelam dan menaklukan samudra php
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Tutorial Pembuatan Aplikasi Website Beserta Databasenya
Tutorial Pembuatan Aplikasi Website Beserta DatabasenyaTutorial Pembuatan Aplikasi Website Beserta Databasenya
Tutorial Pembuatan Aplikasi Website Beserta Databasenya
 
Php
PhpPhp
Php
 
Linux.ppt
Linux.ppt Linux.ppt
Linux.ppt
 

Similaire à SQL -PHP Tutorial

Similaire à SQL -PHP Tutorial (20)

Mysql
MysqlMysql
Mysql
 
Real life-coffeescript
Real life-coffeescriptReal life-coffeescript
Real life-coffeescript
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
PHP tips by a MYSQL DBA
PHP tips by a MYSQL DBAPHP tips by a MYSQL DBA
PHP tips by a MYSQL DBA
 
PHP MySQL
PHP MySQLPHP MySQL
PHP MySQL
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Diva10
Diva10Diva10
Diva10
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
 
12-security.ppt - PHP and Arabic Language - Index
12-security.ppt - PHP and Arabic Language - Index12-security.ppt - PHP and Arabic Language - Index
12-security.ppt - PHP and Arabic Language - Index
 
Security.ppt
Security.pptSecurity.ppt
Security.ppt
 
Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9Open Source Package Php Mysql 1228203701094763 9
Open Source Package Php Mysql 1228203701094763 9
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Porting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQLPorting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQL
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Mysql
MysqlMysql
Mysql
 
DataMapper
DataMapperDataMapper
DataMapper
 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
 
Php MySql For Beginners
Php MySql For BeginnersPhp MySql For Beginners
Php MySql For Beginners
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Php
PhpPhp
Php
 

Plus de Information Technology (20)

Web303
Web303Web303
Web303
 
Sql Server Security Best Practices
Sql Server Security Best PracticesSql Server Security Best Practices
Sql Server Security Best Practices
 
SAN
SANSAN
SAN
 
SAN Review
SAN ReviewSAN Review
SAN Review
 
SQL 2005 Disk IO Performance
SQL 2005 Disk IO PerformanceSQL 2005 Disk IO Performance
SQL 2005 Disk IO Performance
 
RAID Review
RAID ReviewRAID Review
RAID Review
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
Sql 2005 high availability
Sql 2005 high availabilitySql 2005 high availability
Sql 2005 high availability
 
IIS 7: The Administrator’s Guide
IIS 7: The Administrator’s GuideIIS 7: The Administrator’s Guide
IIS 7: The Administrator’s Guide
 
MOSS 2007 Deployment Fundamentals -Part2
MOSS 2007 Deployment Fundamentals -Part2MOSS 2007 Deployment Fundamentals -Part2
MOSS 2007 Deployment Fundamentals -Part2
 
MOSS 2007 Deployment Fundamentals -Part1
MOSS 2007 Deployment Fundamentals -Part1MOSS 2007 Deployment Fundamentals -Part1
MOSS 2007 Deployment Fundamentals -Part1
 
Clustering and High Availability
Clustering and High Availability Clustering and High Availability
Clustering and High Availability
 
F5 beyond load balancer (nov 2009)
F5 beyond load balancer (nov 2009)F5 beyond load balancer (nov 2009)
F5 beyond load balancer (nov 2009)
 
WSS 3.0 & SharePoint 2007
WSS 3.0 & SharePoint 2007WSS 3.0 & SharePoint 2007
WSS 3.0 & SharePoint 2007
 
SharePoint Topology
SharePoint Topology SharePoint Topology
SharePoint Topology
 
Sharepoint Deployments
Sharepoint DeploymentsSharepoint Deployments
Sharepoint Deployments
 
Microsoft Clustering
Microsoft ClusteringMicrosoft Clustering
Microsoft Clustering
 
Scalable Internet Servers and Load Balancing
Scalable Internet Servers and Load BalancingScalable Internet Servers and Load Balancing
Scalable Internet Servers and Load Balancing
 
Web Hacking
Web HackingWeb Hacking
Web Hacking
 
Migration from ASP to ASP.NET
Migration from ASP to ASP.NETMigration from ASP to ASP.NET
Migration from ASP to ASP.NET
 

Dernier

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 

Dernier (20)

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 

SQL -PHP Tutorial

  • 1. SQL | PHP Tutorial at 8am. god, it’s early.
  • 2.
  • 3. Databases _ creation CREATE TABLE tableName ( name VARCHAR(55), sex CHAR(1), age INT(3), birthdate DATE, salary DECIMAL(10,2), primary key(name) ); Types of attributes: char, varchar, int, smallint, decimal, date, float, etc. * varchar is a string with varying # of characters. In our example, 55 is the characters longest possible string allowed. * decimal(10,2) indicated 2 places after the decimal point and 10 total digits (including the decimal numbers)
  • 4. Databases _ creation 2 CREATE TABLE tableName ( name VARCHAR(55), sex CHAR(1) NOT NULL, age INT(3), birthdate DATE, salary DECIMAL(10,2) DEFAULT ‘0.00’, primary key(name) ); Primary key: primary key is a UNIQUE value. For every entry in your database this must be unique and not null and every DB must have one. NOT NULL: column must have a value DEFAULT: you can set a default value if no other value is inputted for that column.
  • 5. Databases _ indexed primary keys Instead of specifying a column as a primary key you can have the database create a column of numbers that will automatically increment with each entry inserted into the DB. Example: CREATE TABLE tableName ( id INT AUTO_INCREMENT , name VARCHAR(55), sex CHAR(1), age INT(3), birthdate DATE, salary DECIMAL(10,2), primary key(id) ); Entry 1 will have 1 as a key. Entry 2 will have 2 and so forth.
  • 6. Databases _ deletion DROP TABLE tableName;
  • 7. Databases _ insertion Inserting data in the database: INSERT INTO tableName(name,sex,age) VALUES(‘Mr. Freeze’,’M’,42); Also valid: INSERT INTO tableName(sex,name,age) VALUES(‘F’,’Mr. Freeze’,42); Order doesn’t matter.
  • 8. Databases _ the meat Always in the form of: SELECT …. FROM …. WHERE …. So select a column from your database. From a database Where x meets y condition. * Except in the case of modification
  • 9. Databases _ updating Suppose we want to change Mr. Freeze’s age to 52. UPDATE tableName SET age = ’52’ WHERE name LIKE ‘Mr. Freeze’ And so forth.
  • 10. Databases _ aggregates This is the actual meat of using SQL. These are where you set your conditions, narrow down your table into a usable set. Here are the usable functions, I’ll show a quick example with each. The only way to really know this stuff is practice. Group by Count Sum Avg Min/Max Order by
  • 11. Databases _ group by This is the actual meat of using SQL. These are where you set your conditions, narrow down your table into a usable set. Here are the usable functions, I’ll show a quick example with each. The only way to really know this stuff is practice. Group by lumps all the common attributes into one row. SELECT employee_id, MAX(salary) FROM Works_In GROUP BY dept_id; * MAX selects the maximum value in its () likewise for MIN
  • 12. Databases _ count Count counts the number of columns with the specified attribute. SELECT term, COUNT(course_id) FROM teaches GROUP BY term; We counted the number of courses taught during x term. AVG & SUM function pretty much the same way.
  • 13.  
  • 14. PHP _ connecting to the db This is the basic connect script for accessing your db: <?php mysql_connect(“localhost”,”username”,”password”) or die(mysql_error()); ?> Localhost indicates the current machine. So you’re asking the machine to connect to itself. The die(mysql_error) part says if there’s an error halt everything and display this error. If it errors on this part, it means either your host, username, or password are wrong.
  • 15. PHP _ error checking w/ echo Consider the connection script again with this modification: <?php mysql_connect(“localhost”,”username”,”password”) or die(mysql_error()); echo “Connected to database.” ?> Later on you may be unable to differentiate where the error occurred. So while developing your code throw in some echo statements, they just print stuff to the screen. When PHP is done connecting to our database it tell us.
  • 16. PHP _ select the database. <?php mysql_connect(“localhost”,”username”,”password”) or die(mysql_error()); echo “Connected MySQL!”; mysql_select_db(“ljlayou_comp353” or die(mysql_error()); echo “Connected to database 353”; ?>
  • 17. PHP _ create/drop table <?php mysql_connect(“localhost”,”username”,”pw”) or die(mysql_error()); mysql_select_db(“ljlayou_comp353” or die(mysql_error()); mysql_query(“CREATE TABLE Works_In(…)“) or die(mysql_error()); ?> We’re querying PHP to tell MySQL to do something, in this case create the table. The same applies for dropping a table. As you can see our code is being reused over and over. It gets pretty repetitive like this. Again we tell php to stop everything if an error occurs.
  • 18. PHP _ insertion <?php mysql_connect(“localhost”,”username”,”pw”) or die(mysql_error()); mysql_select_db(“ljlayou_comp353” or die(mysql_error()); mysql_query(“INSERT INTO Works_In(company,position) VALUES(‘McDonalds’,’fry cook’)”); ?> We’re querying PHP to tell MySQL to do something, in this case create the table. The same applies for dropping a table. As you can see our code is being reused over and over. It gets pretty repetitive like this.
  • 19. PHP _ selecting a table In order to manipulate, fetch, etc data from your database you must have PHP remember the result. So we store it in an array (?) to preserve “columns”. PHP variables unlike Java do not need a type declaration. From now on I’ll be omitting the connect stuff. <?php […] $result = mysql_query(“SELECT * FROM Works_In”) or die(mysql_error()); $row = mysql_fetch_array($result); echo “company: “ .$row[‘company’]; echo “position:” .$row[‘position’]; ?> From these lines we see that each cell in the area is labeled under the column name. Using this method we can output or even compare data.
  • 20. PHP _ selecting a table In order to manipulate, fetch, etc data from your database you must have PHP remember the result. So we store it in an array (?) to preserve “columns”. PHP variables unlike Java do not need a type declaration. From now on I’ll be omitting the connect stuff. <?php […] $result = mysql_query(“SELECT * FROM Works_In”) or die(mysql_error()); $row = mysql_fetch_array($result); echo “company: “ .$row[‘company’]; echo “position:” .$row[‘position’]; ?> From these lines we see that each cell in the area is labeled under the column name. Using this method we can output or even compare data. The ‘*’ symbol in the SELECT statement just means that we select all the columns in the table. The above statement however results in the first row only being shown.
  • 21. PHP _ selecting a table 2 To solve this problem, we loop continuously until there are no more rows to choose from. <?php […] while ($row = mysql_fetch_array($result)) { echo “company: “ .$row[‘company’]. “ | “position:” .$row[‘position’]; echo “<br/>”;} ?> If you have noticed the ‘.’ symbol signifies a concatenation.
  • 22. PHP _ the formula We looked over it all. Here’s the general formula: <?php mysql_connect(“ localhost ”,” username ”,” pw ”) or die(mysql_error()); mysql_select_db(“ databaseName ” or die(mysql_error()); $result = mysql_query( yourQuery ) or die(mysql_error()); $row = mysql_fetch_array($result); while ($row = mysql_fetch_array($result)) { … }; ?> (Show 255 final)
  • 23.  
  • 24. PHP _ form processing Topic job but it’s all good. I’m assuming you know how to create forms in HTML. Else, well, google it. It’s pretty straight forward. So let’s take this form as our example, this is a snippet of the form code: <form name=“animas” action=“processform.php&quot; method=&quot;post”> […] <b>FUN</b>: <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;horns&quot;>Horns <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;wings&quot;>Wings <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;mane&quot;>Mane <input type=&quot;radio&quot; name=&quot;extra&quot; value=&quot;giraffe&quot;>Giraffe Neck <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;imagine it&quot; class=&quot;submit&quot; /> </form>
  • 25. PHP _ form processing 2 Our form action tells the form what to do with the data. POST is a method that sends an array of variables, our data. Only when the submit button is pressed is the data sent. <form name=“animals” action=“processform.php&quot; method=&quot;post”> […] <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;imagine it&quot; class=&quot;submit&quot; /> </form> It is common practice to create a separate file for form processing.
  • 26. PHP _ form processing 3 Our data is now winding through the PHP tubes. Let’s look how it’s processed. $_POST[‘submit’] if( $dbc = @mysql_connect(‘localhost’,’username’,’pw’)) { if(!@mysql_select_db(‘database’)) { die(mysql_error());} } else { die(mysql_error());} $query = “INSERT INTO animals(id,data,appendages, tail, sound,extra) VALUES(0,’{$_POST[‘body’]}’,’{$_POST[‘appendages’]}’, […] )” if(@mysql_query($query) { print “success”;} else { print “you lose”;} mysql_close();
  • 27.