SlideShare a Scribd company logo
Basic PHP
Database
programming
Hello!I am Dave Stokes
MySQl Community Manager
FORTRAN, Punch Cards, ‘Personal Home Page’, older than dirt, etc.
22 Years old!
MySQL 5.7 relased ~ 2 years ago
-- JSON Data Tyoe
MySQL 8 Developer Milestone
Release
-- Available for testing
-- Data dictionary
-- UTf8MB4
More Plug-in Features
-- Group replication
-- Document Store
Oracle MySQL Cloud
-- Enterprise edition of
software
1.
Basics
Client Server Model and simple
connections
Using MySQL
but concepts
will transpose
over to other
Relational
Database
Management
Systems.
● Network – tcp/ip
● Port 3306
● Windows, Mac, Linux,
& source code
+ containers
To connect you will need
0. Server name or IP address
1. Persmission to connect
2. Account/password
3. (more later)
<?php
$mysqli = new mysqli("127.0.0.1", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
echo $mysqli->host_info . "n";
?>
<?php
$mysqli = new mysqli("127.0.0.1", "user", "password", "database");
Host – 127.0.0.1 (or 192.168.10.11 or db.foobar.com)
User – User on the MySQL server (mysql.user table), not OS user
Password – Clear text (other options later) -> Security issue
Database – Schema to be used. Can be selected/changed later
Port (optional) – defaults to 3306
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " .
$mysqli->connect_error;
}
If MySQL there is a MySQL connection error
then provide details
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
● Resource Guide
● Examples
● First place to look for answers
Syntax
Programming is hard but the syntactical aspects are
usually simple (some times too simple)
2.
Get Data
After connecting we need to do
something
1970’s
Disks are expensive and slow
Relational Model emerges with
goal of efficiency
Data Normaliation to berak
data in smaller logically
consistent groups
SQL was designed at IBM
to provide efficient
access to data via a
descriptive language
based on relational
calulus.
if ($result = $mysqli->query("SELECT * FROM City")) {
printf("Select returned %d rows.n", $result->num_rows);
/* free result set */
$result->close();
}
$mysqli->close();
?>
SELECT * FROM City
• * is a wild card for ‘all column in table
• City is the table being queried
• SELECT is the action (SELECT, UPDATE, DELETE, etc)
$mysqli = new mysqli();
$result = $mysqli->query();
Do Something with the data in app
$result->close();
$mysqli->close();
The basic flow is very
simple – connect,
query, close.
Note: the above is without return codes!
Always check return codes!
SELECT * FROM City
$result = $mysqli->query("SELECT id, label FROM test WHERE id = 1");
$row = $result->fetch_assoc();
printf("id = %s (%s)n", $row['id'], gettype($row['id']));
printf("label = %s (%s)n", $row['label'], gettype($row['label']));
$row = $result->fetch_assoc();
fetch_assoc -- Fetch a result row as an associative array
fetch_array() Fetch a result row as an associative, a numeric
array, or both
fetch_row() Get a result row as an enumerated array
fetch_object() Returns the current row of a result set as an
object
Congratulations! You know have the BASICS
down. The bad news is that there are are lot of
other things to learn.
The longest journey begins
with getting up off your
backside!!
3.
INput Data
Here is where being careful pays off
NEVER EVER TRUST
data input from a user!
Period!
EVER!
INTEGERS
Check to see if
intergers are really
integers by casting
them! Range check if
you can!!
STRINGS
Check size, filter out
junk, and expect the
unexpected!!
FILES
Isolate, scan, and
doublecheck.
Paranoia in defense of your data is a virtue not a sin
Yes, there are people out there that would love
to mess up your work, scramble or delete your
data, usurp your server, and in general treat you
professional life like a Games of Thrones plot
line including a Lannister but you are not a
Lannister
$QUERY = “SELECT * FROM PayingCustomers WERE userId = $id”;
if (!$mysqli->query($QUERY) {
echo “Query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
Can you spot TWO PROBLEMS with the above
(assume $email is from a form filled in by user) ?
What if $id is =
‘me@mw.com’ or 1 = 1
EVALUATES as TRUE
SELECT * FROM Users WHERE Name ="John Doe"
AND Pass ="Pass“
Name and Pass = " or ""="
SELECT * FROM Users WHERE Name ="" or ""=""
AND Pass ="" or ""=""
SELECT * FROM Users WHERE UserId = $UserId
$UserId = 15; DROP TABLE suppliers
if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$id = 1;
if (!$stmt->bind_param("i", $id)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
for ($id = 2; $id < 5; $id++) {
if (!$stmt->execute()) {
echo "Execute failed:(" . $stmt->errno . ") " . $stmt-
>error;
}
}
4.
The dreaded N+1
Problem
Killing your database server
performance by a thousand cuts
Problem: You need a ride to work tomorrow and need to find an active
employee that lives in your city.
Query 1 – Find the active employees in your city.
Query 2 – Of those employees, find those who have a parking permit.
Versus
Query1 -- Find employees in your city that have a parking permit
Problem: You need to find all customers with unfulfilled orders over thirty days
that are not ready to ship and have already paid. A series of small queries is
doing to take more resources and time that one big query.
Each dive into the data has a cost – minimize!!
Let the database do the ‘heavy lifting’ – that is its purpose!
Every time you connect and send a query to a MySQL server it will check:
1. Is your computer allowed to connect?
2. Is your account/authentication-string valid?
3. Do you have permission to access the data requested?
This adds up with small queries
Whew!The is the basic basics!
1. SQL Anti Patterns:SQL Antipatterns: Avoiding the Pitfalls of Database Programming
Bill Karwin
2. Database Design and Relational Theory: Normal Forms and All That Jazz
CJ Date
THANKS!Any questions?
You can find me at @stoker or david.stokes @ oracle.com
Elephantdolphin.blogger.com
Slideshare.net/davidmstokes https://joind.in/talk/ad37a
Ad

More Related Content

What's hot (20)

Php 2
Php 2Php 2
Php 2
tnngo2
 
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
FYBSC IT Web Programming Unit V  Advanced PHP and MySQLFYBSC IT Web Programming Unit V  Advanced PHP and MySQL
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
ichikaway
 
Sequelize
SequelizeSequelize
Sequelize
Tarek Raihan
 
Coffeescript a z
Coffeescript a zCoffeescript a z
Coffeescript a z
Starbuildr
 
DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009
Dirkjan Bussink
 
Object oriented mysqli connection function
Object oriented mysqli connection functionObject oriented mysqli connection function
Object oriented mysqli connection function
clickon2010
 
Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016
Colin O'Dell
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL
Hung-yu Lin
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a database
Leon Fayer
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performance
Leon Fayer
 
Couchdb
CouchdbCouchdb
Couchdb
Brian Smith
 
Rails 3 ActiveRecord
Rails 3 ActiveRecordRails 3 ActiveRecord
Rails 3 ActiveRecord
Blazing Cloud
 
Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.
fRui Apps
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
Inbal Geffen
 
Getting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainGetting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, Again
DrewAPicture
 
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa HallPitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
hannonhill
 
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
MooseX::Datamodel - Barcelona Perl Workshop Lightning talkMooseX::Datamodel - Barcelona Perl Workshop Lightning talk
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
Jose Luis Martínez
 
PostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL SuperpowersPostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL Superpowers
Amanda Gilmore
 
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
FYBSC IT Web Programming Unit V  Advanced PHP and MySQLFYBSC IT Web Programming Unit V  Advanced PHP and MySQL
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
ichikaway
 
Coffeescript a z
Coffeescript a zCoffeescript a z
Coffeescript a z
Starbuildr
 
DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009
Dirkjan Bussink
 
Object oriented mysqli connection function
Object oriented mysqli connection functionObject oriented mysqli connection function
Object oriented mysqli connection function
clickon2010
 
Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016Hacking Your Way To Better Security - php[tek] 2016
Hacking Your Way To Better Security - php[tek] 2016
Colin O'Dell
 
Web2py Code Lab
Web2py Code LabWeb2py Code Lab
Web2py Code Lab
Colin Su
 
2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL2014 database - course 3 - PHP and MySQL
2014 database - course 3 - PHP and MySQL
Hung-yu Lin
 
PHP performance 101: so you need to use a database
PHP performance 101: so you need to use a databasePHP performance 101: so you need to use a database
PHP performance 101: so you need to use a database
Leon Fayer
 
Developing applications for performance
Developing applications for performanceDeveloping applications for performance
Developing applications for performance
Leon Fayer
 
Rails 3 ActiveRecord
Rails 3 ActiveRecordRails 3 ActiveRecord
Rails 3 ActiveRecord
Blazing Cloud
 
Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.Web2py tutorial to create db driven application.
Web2py tutorial to create db driven application.
fRui Apps
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
Inbal Geffen
 
Getting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, AgainGetting Creative with WordPress Queries, Again
Getting Creative with WordPress Queries, Again
DrewAPicture
 
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa HallPitfalls to Avoid for Cascade Server Newbies by Lisa Hall
Pitfalls to Avoid for Cascade Server Newbies by Lisa Hall
hannonhill
 
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
MooseX::Datamodel - Barcelona Perl Workshop Lightning talkMooseX::Datamodel - Barcelona Perl Workshop Lightning talk
MooseX::Datamodel - Barcelona Perl Workshop Lightning talk
Jose Luis Martínez
 
PostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL SuperpowersPostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL Superpowers
Amanda Gilmore
 

Similar to PHP Database Programming Basics -- Northeast PHP (20)

DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
lecture 7 - Introduction to MySQL with PHP.pptx
lecture 7 - Introduction to  MySQL with PHP.pptxlecture 7 - Introduction to  MySQL with PHP.pptx
lecture 7 - Introduction to MySQL with PHP.pptx
AOmaAli
 
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
MySQL Without the SQL -- Oh My!  Longhorn PHP ConferenceMySQL Without the SQL -- Oh My!  Longhorn PHP Conference
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
Dave Stokes
 
MySQL Without the MySQL -- Oh My!
MySQL Without the MySQL -- Oh My!MySQL Without the MySQL -- Oh My!
MySQL Without the MySQL -- Oh My!
Dave Stokes
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
Wim Godden
 
Php summary
Php summaryPhp summary
Php summary
Michelle Darling
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
Wim Godden
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptxBuilding an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Altinity Ltd
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open SourceBuilding an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open Source
Altinity Ltd
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
Jay Shirley
 
Stored Procedure
Stored ProcedureStored Procedure
Stored Procedure
NidiaRamirez07
 
Cassandra & puppet, scaling data at $15 per month
Cassandra & puppet, scaling data at $15 per monthCassandra & puppet, scaling data at $15 per month
Cassandra & puppet, scaling data at $15 per month
daveconnors
 
Php Security - OWASP
Php  Security - OWASPPhp  Security - OWASP
Php Security - OWASP
Mizno Kruge
 
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
 
Web Security - Hands-on
Web Security - Hands-onWeb Security - Hands-on
Web Security - Hands-on
Andrea Valenza
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
MySQL server security
MySQL server securityMySQL server security
MySQL server security
Damien Seguy
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power StackRails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power Stack
David Copeland
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
Functional Thursday
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
Sina Manavi
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
lecture 7 - Introduction to MySQL with PHP.pptx
lecture 7 - Introduction to  MySQL with PHP.pptxlecture 7 - Introduction to  MySQL with PHP.pptx
lecture 7 - Introduction to MySQL with PHP.pptx
AOmaAli
 
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
MySQL Without the SQL -- Oh My!  Longhorn PHP ConferenceMySQL Without the SQL -- Oh My!  Longhorn PHP Conference
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
Dave Stokes
 
MySQL Without the MySQL -- Oh My!
MySQL Without the MySQL -- Oh My!MySQL Without the MySQL -- Oh My!
MySQL Without the MySQL -- Oh My!
Dave Stokes
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
Wim Godden
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
Wim Godden
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptxBuilding an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Building an Analytic Extension to MySQL with ClickHouse and Open Source.pptx
Altinity Ltd
 
Building an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open SourceBuilding an Analytic Extension to MySQL with ClickHouse and Open Source
Building an Analytic Extension to MySQL with ClickHouse and Open Source
Altinity Ltd
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
Jay Shirley
 
Cassandra & puppet, scaling data at $15 per month
Cassandra & puppet, scaling data at $15 per monthCassandra & puppet, scaling data at $15 per month
Cassandra & puppet, scaling data at $15 per month
daveconnors
 
Php Security - OWASP
Php  Security - OWASPPhp  Security - OWASP
Php Security - OWASP
Mizno Kruge
 
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
 
Web Security - Hands-on
Web Security - Hands-onWeb Security - Hands-on
Web Security - Hands-on
Andrea Valenza
 
MySQL server security
MySQL server securityMySQL server security
MySQL server security
Damien Seguy
 
Rails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power StackRails, Postgres, Angular, and Bootstrap: The Power Stack
Rails, Postgres, Angular, and Bootstrap: The Power Stack
David Copeland
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
Functional Thursday
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
Sina Manavi
 
Ad

More from Dave Stokes (20)

Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Valkey 101 - SCaLE 22x March 2025 Stokes.pdfValkey 101 - SCaLE 22x March 2025 Stokes.pdf
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Dave Stokes
 
Locking Down Your MySQL Database.pptx
Locking Down Your MySQL Database.pptxLocking Down Your MySQL Database.pptx
Locking Down Your MySQL Database.pptx
Dave Stokes
 
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre FeaturesLinuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Dave Stokes
 
MySQL Indexes and Histograms - RMOUG Training Days 2022
MySQL Indexes and Histograms - RMOUG Training Days 2022MySQL Indexes and Histograms - RMOUG Training Days 2022
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dave Stokes
 
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Dave Stokes
 
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019
Dave Stokes
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Dave Stokes
 
Develop PHP Applications with MySQL X DevAPI
Develop PHP Applications with MySQL X DevAPIDevelop PHP Applications with MySQL X DevAPI
Develop PHP Applications with MySQL X DevAPI
Dave Stokes
 
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MySQL 8 Tips and Tricks from Symfony USA 2018, San FranciscoMySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
Dave Stokes
 
The Proper Care and Feeding of MySQL Databases
The Proper Care and Feeding of MySQL DatabasesThe Proper Care and Feeding of MySQL Databases
The Proper Care and Feeding of MySQL Databases
Dave Stokes
 
MySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHPMySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHP
Dave Stokes
 
MySQL 8 Server Optimization Swanseacon 2018
MySQL 8 Server Optimization Swanseacon 2018MySQL 8 Server Optimization Swanseacon 2018
MySQL 8 Server Optimization Swanseacon 2018
Dave Stokes
 
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
Dave Stokes
 
Presentation Skills for Open Source Folks
Presentation Skills for Open Source FolksPresentation Skills for Open Source Folks
Presentation Skills for Open Source Folks
Dave Stokes
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
Dave Stokes
 
ConFoo MySQL Replication Evolution : From Simple to Group Replication
ConFoo  MySQL Replication Evolution : From Simple to Group ReplicationConFoo  MySQL Replication Evolution : From Simple to Group Replication
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Dave Stokes
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
Dave Stokes
 
Making MySQL Agile-ish
Making MySQL Agile-ishMaking MySQL Agile-ish
Making MySQL Agile-ish
Dave Stokes
 
MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017
Dave Stokes
 
MySQL Replication Evolution -- Confoo Montreal 2017
MySQL Replication Evolution -- Confoo Montreal 2017MySQL Replication Evolution -- Confoo Montreal 2017
MySQL Replication Evolution -- Confoo Montreal 2017
Dave Stokes
 
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Valkey 101 - SCaLE 22x March 2025 Stokes.pdfValkey 101 - SCaLE 22x March 2025 Stokes.pdf
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Dave Stokes
 
Locking Down Your MySQL Database.pptx
Locking Down Your MySQL Database.pptxLocking Down Your MySQL Database.pptx
Locking Down Your MySQL Database.pptx
Dave Stokes
 
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre FeaturesLinuxfest Northwest 2022 - MySQL 8.0 Nre Features
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Dave Stokes
 
MySQL Indexes and Histograms - RMOUG Training Days 2022
MySQL Indexes and Histograms - RMOUG Training Days 2022MySQL Indexes and Histograms - RMOUG Training Days 2022
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dave Stokes
 
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Dave Stokes
 
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech fest 2019
Dave Stokes
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Dave Stokes
 
Develop PHP Applications with MySQL X DevAPI
Develop PHP Applications with MySQL X DevAPIDevelop PHP Applications with MySQL X DevAPI
Develop PHP Applications with MySQL X DevAPI
Dave Stokes
 
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MySQL 8 Tips and Tricks from Symfony USA 2018, San FranciscoMySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
Dave Stokes
 
The Proper Care and Feeding of MySQL Databases
The Proper Care and Feeding of MySQL DatabasesThe Proper Care and Feeding of MySQL Databases
The Proper Care and Feeding of MySQL Databases
Dave Stokes
 
MySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHPMySQL without the SQL -- Cascadia PHP
MySQL without the SQL -- Cascadia PHP
Dave Stokes
 
MySQL 8 Server Optimization Swanseacon 2018
MySQL 8 Server Optimization Swanseacon 2018MySQL 8 Server Optimization Swanseacon 2018
MySQL 8 Server Optimization Swanseacon 2018
Dave Stokes
 
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
Dave Stokes
 
Presentation Skills for Open Source Folks
Presentation Skills for Open Source FolksPresentation Skills for Open Source Folks
Presentation Skills for Open Source Folks
Dave Stokes
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
Dave Stokes
 
ConFoo MySQL Replication Evolution : From Simple to Group Replication
ConFoo  MySQL Replication Evolution : From Simple to Group ReplicationConFoo  MySQL Replication Evolution : From Simple to Group Replication
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Dave Stokes
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
Dave Stokes
 
Making MySQL Agile-ish
Making MySQL Agile-ishMaking MySQL Agile-ish
Making MySQL Agile-ish
Dave Stokes
 
MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017MySQL 101 PHPTek 2017
MySQL 101 PHPTek 2017
Dave Stokes
 
MySQL Replication Evolution -- Confoo Montreal 2017
MySQL Replication Evolution -- Confoo Montreal 2017MySQL Replication Evolution -- Confoo Montreal 2017
MySQL Replication Evolution -- Confoo Montreal 2017
Dave Stokes
 
Ad

Recently uploaded (20)

Internet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) ReviewInternet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) Review
APNIC
 
Global Networking Trends, presented at TWNIC 43rd IP Open Policy Meeting
Global Networking Trends, presented at TWNIC 43rd IP Open Policy MeetingGlobal Networking Trends, presented at TWNIC 43rd IP Open Policy Meeting
Global Networking Trends, presented at TWNIC 43rd IP Open Policy Meeting
APNIC
 
34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf
Nguyễn Minh
 
Cloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptxCloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptx
marketing140789
 
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC
 
34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf
Nguyễn Minh
 
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
Taqyea
 
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptxBiochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
SergioBarreno2
 
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
Nguyễn Minh
 
Breaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdfBreaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdf
Internet Bundle Now
 
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
Nguyễn Minh
 
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
Nguyễn Minh
 
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptxFractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
ChaitanJaunky1
 
34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf
Nguyễn Minh
 
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
Taqyea
 
34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf
34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf
34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf
Nguyễn Minh
 
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and MonitoringPresentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
mdaoudi
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
Taqyea
 
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness GuideThe Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
russellpeter1995
 
Internet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) ReviewInternet Coordination Policy 2 (ICP-2) Review
Internet Coordination Policy 2 (ICP-2) Review
APNIC
 
Global Networking Trends, presented at TWNIC 43rd IP Open Policy Meeting
Global Networking Trends, presented at TWNIC 43rd IP Open Policy MeetingGlobal Networking Trends, presented at TWNIC 43rd IP Open Policy Meeting
Global Networking Trends, presented at TWNIC 43rd IP Open Policy Meeting
APNIC
 
34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf34 Advances in Mobile Commerce Technologies (2003).pdf
34 Advances in Mobile Commerce Technologies (2003).pdf
Nguyễn Minh
 
Cloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptxCloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptx
marketing140789
 
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC Policy Update and Participation, presented at TWNIC 43rd IP Open Policy...
APNIC
 
34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf34 Mobile Payment (Thomas Lerner (auth.).pdf
34 Mobile Payment (Thomas Lerner (auth.).pdf
Nguyễn Minh
 
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
Taqyea
 
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptxBiochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
Biochemistry and Biomolecules - Science - 9th Grade _ by Slidesgo.pptx
SergioBarreno2
 
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
34 Mobile Electronic Commerce_ Foundations, Development, and Applications (20...
Nguyễn Minh
 
Breaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdfBreaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdf
Internet Bundle Now
 
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
34 Global Mobile Commerce_ Strategies, Implementation and Case Studies (Premi...
Nguyễn Minh
 
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
23 Introduction to E-Commerce ( PDFDrive ) (1).pdf
Nguyễn Minh
 
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptxFractures In Chronic Kidney Disease Patients - Copy (3).pptx
Fractures In Chronic Kidney Disease Patients - Copy (3).pptx
ChaitanJaunky1
 
34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf34 E-commerce - business, technology and society (2022).pdf
34 E-commerce - business, technology and society (2022).pdf
Nguyễn Minh
 
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
水印成绩单加拿大Mohawk文凭莫霍克学院在读证明毕业证
Taqyea
 
34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf
34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf
34 E-commerce and M-commerce technologies (P. Candace Deans 2006).pdf
Nguyễn Minh
 
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and MonitoringPresentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
mdaoudi
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
Taqyea
 
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness GuideThe Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
russellpeter1995
 

PHP Database Programming Basics -- Northeast PHP

  • 2. Hello!I am Dave Stokes MySQl Community Manager FORTRAN, Punch Cards, ‘Personal Home Page’, older than dirt, etc.
  • 3. 22 Years old! MySQL 5.7 relased ~ 2 years ago -- JSON Data Tyoe MySQL 8 Developer Milestone Release -- Available for testing -- Data dictionary -- UTf8MB4 More Plug-in Features -- Group replication -- Document Store Oracle MySQL Cloud -- Enterprise edition of software
  • 4. 1. Basics Client Server Model and simple connections Using MySQL but concepts will transpose over to other Relational Database Management Systems.
  • 5. ● Network – tcp/ip ● Port 3306 ● Windows, Mac, Linux, & source code + containers
  • 6. To connect you will need 0. Server name or IP address 1. Persmission to connect 2. Account/password 3. (more later)
  • 7. <?php $mysqli = new mysqli("127.0.0.1", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo $mysqli->host_info . "n"; ?>
  • 8. <?php $mysqli = new mysqli("127.0.0.1", "user", "password", "database"); Host – 127.0.0.1 (or 192.168.10.11 or db.foobar.com) User – User on the MySQL server (mysql.user table), not OS user Password – Clear text (other options later) -> Security issue Database – Schema to be used. Can be selected/changed later Port (optional) – defaults to 3306
  • 9. if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } If MySQL there is a MySQL connection error then provide details
  • 10. <?php $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
  • 11. ● Resource Guide ● Examples ● First place to look for answers
  • 12. Syntax Programming is hard but the syntactical aspects are usually simple (some times too simple)
  • 13. 2. Get Data After connecting we need to do something
  • 14. 1970’s Disks are expensive and slow Relational Model emerges with goal of efficiency Data Normaliation to berak data in smaller logically consistent groups SQL was designed at IBM to provide efficient access to data via a descriptive language based on relational calulus.
  • 15. if ($result = $mysqli->query("SELECT * FROM City")) { printf("Select returned %d rows.n", $result->num_rows); /* free result set */ $result->close(); } $mysqli->close(); ?>
  • 16. SELECT * FROM City • * is a wild card for ‘all column in table • City is the table being queried • SELECT is the action (SELECT, UPDATE, DELETE, etc)
  • 17. $mysqli = new mysqli(); $result = $mysqli->query(); Do Something with the data in app $result->close(); $mysqli->close(); The basic flow is very simple – connect, query, close. Note: the above is without return codes! Always check return codes!
  • 18. SELECT * FROM City $result = $mysqli->query("SELECT id, label FROM test WHERE id = 1"); $row = $result->fetch_assoc(); printf("id = %s (%s)n", $row['id'], gettype($row['id'])); printf("label = %s (%s)n", $row['label'], gettype($row['label']));
  • 19. $row = $result->fetch_assoc(); fetch_assoc -- Fetch a result row as an associative array fetch_array() Fetch a result row as an associative, a numeric array, or both fetch_row() Get a result row as an enumerated array fetch_object() Returns the current row of a result set as an object
  • 20. Congratulations! You know have the BASICS down. The bad news is that there are are lot of other things to learn.
  • 21. The longest journey begins with getting up off your backside!!
  • 22. 3. INput Data Here is where being careful pays off
  • 23. NEVER EVER TRUST data input from a user! Period! EVER!
  • 24. INTEGERS Check to see if intergers are really integers by casting them! Range check if you can!! STRINGS Check size, filter out junk, and expect the unexpected!! FILES Isolate, scan, and doublecheck. Paranoia in defense of your data is a virtue not a sin
  • 25. Yes, there are people out there that would love to mess up your work, scramble or delete your data, usurp your server, and in general treat you professional life like a Games of Thrones plot line including a Lannister but you are not a Lannister
  • 26. $QUERY = “SELECT * FROM PayingCustomers WERE userId = $id”; if (!$mysqli->query($QUERY) { echo “Query failed: (" . $mysqli->errno . ") " . $mysqli->error; } Can you spot TWO PROBLEMS with the above (assume $email is from a form filled in by user) ?
  • 27. What if $id is = ‘me@mw.com’ or 1 = 1 EVALUATES as TRUE
  • 28. SELECT * FROM Users WHERE Name ="John Doe" AND Pass ="Pass“ Name and Pass = " or ""=" SELECT * FROM Users WHERE Name ="" or ""="" AND Pass ="" or ""=""
  • 29. SELECT * FROM Users WHERE UserId = $UserId $UserId = 15; DROP TABLE suppliers
  • 30. if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) { echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; } $id = 1; if (!$stmt->bind_param("i", $id)) { echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; } if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; }
  • 31. for ($id = 2; $id < 5; $id++) { if (!$stmt->execute()) { echo "Execute failed:(" . $stmt->errno . ") " . $stmt- >error; } }
  • 32. 4. The dreaded N+1 Problem Killing your database server performance by a thousand cuts
  • 33. Problem: You need a ride to work tomorrow and need to find an active employee that lives in your city. Query 1 – Find the active employees in your city. Query 2 – Of those employees, find those who have a parking permit. Versus Query1 -- Find employees in your city that have a parking permit
  • 34. Problem: You need to find all customers with unfulfilled orders over thirty days that are not ready to ship and have already paid. A series of small queries is doing to take more resources and time that one big query. Each dive into the data has a cost – minimize!! Let the database do the ‘heavy lifting’ – that is its purpose!
  • 35. Every time you connect and send a query to a MySQL server it will check: 1. Is your computer allowed to connect? 2. Is your account/authentication-string valid? 3. Do you have permission to access the data requested? This adds up with small queries
  • 36. Whew!The is the basic basics!
  • 37. 1. SQL Anti Patterns:SQL Antipatterns: Avoiding the Pitfalls of Database Programming Bill Karwin 2. Database Design and Relational Theory: Normal Forms and All That Jazz CJ Date
  • 38. THANKS!Any questions? You can find me at @stoker or david.stokes @ oracle.com Elephantdolphin.blogger.com Slideshare.net/davidmstokes https://joind.in/talk/ad37a
  翻译: