Your SlideShare is downloading. ×
0
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
PHP 5 + MySQL 5 = A Perfect 10
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

PHP 5 + MySQL 5 = A Perfect 10

3,502

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,502
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
75
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP 5 + MySQL 5 A Perfect 10
  • 2. 1. mysqli extension <ul><li>i is for improved! </li></ul><ul><li>All new MySQL extension for PHP 5 </li></ul><ul><li>Result of </li></ul><ul><ul><li>New binary client protocol in MySQL 4.1 </li></ul></ul><ul><ul><li>Old mysql extension showing age </li></ul></ul><ul><li>Biggest change is that database handle is now mandatory and is the first argument </li></ul>
  • 3. 1. mysqli extension <ul><li>$db = mysqli_connect($server, $user, $password, &amp;quot;users&amp;quot;); </li></ul><ul><li>$r = mysqli_query($db, &amp;quot;SELECT user FROM users&amp;quot;); </li></ul><ul><li>while ($row = mysqli_fetch_assoc($r)) { </li></ul><ul><li>print $row[&apos;user&apos;]; </li></ul><ul><li>} </li></ul><ul><li>mysqli_free_result($r); </li></ul><ul><li>mysqli_close($db); </li></ul>
  • 4. 2. Object-Oriented Interface <ul><li>Create a MySQL object! </li></ul><ul><li>No real advantages over the procedural interface, except that objects are inherently cooler than functions. </li></ul><ul><li>Actually, there are a few neat things you can do by subclassing: </li></ul><ul><ul><li>Create specialized classes </li></ul></ul><ul><ul><li>Redefine methods </li></ul></ul>
  • 5. 2. Object-Oriented Interface <ul><li>$db = new mysqli($server, $user, $password, &amp;quot;users&amp;quot;); </li></ul><ul><li>$r = $db-&gt;query(&amp;quot;SELECT user FROM users&amp;quot;); </li></ul><ul><li>while ($row = $r-&gt;fetch_assoc()) { </li></ul><ul><li>print $row[&apos;user&apos;]; </li></ul><ul><li>} </li></ul><ul><li>$r-&gt;free_result(); </li></ul><ul><li>unset($db); </li></ul>
  • 6. 3. Prepared Statements <ul><li>Define a query “template” </li></ul><ul><li>Faster for MySQL to execute </li></ul><ul><li>Send less data </li></ul><ul><li>Defense against SQL injection attacks </li></ul>
  • 7. 3. Prepared Statements <ul><li>$db = mysqli_connect($server, $user, $password, &apos;stocks&apos;); </li></ul><ul><li>$sql = &apos;SELECT price FROM stocks </li></ul><ul><li>WHERE ticker = ?&apos;; </li></ul><ul><li>$stmt = mysqli_stmt_init($db); </li></ul><ul><li>if (mysqli_stmt_prepare($stmt, $sql)) { </li></ul><ul><li>// More to come… </li></ul><ul><li>} </li></ul>
  • 8. 4. Bound Parameters <ul><li>Map PHP variables with MySQL fields </li></ul><ul><li>Works with stored procedures </li></ul><ul><li>Can bind for both input and output </li></ul><ul><li>Watch your variable scope </li></ul>
  • 9. 4. Bound Parameters <ul><li>$ticker = &apos;EBAY&apos;; </li></ul><ul><li>if (mysqli_stmt_prepare($stmt, $sql)) { </li></ul><ul><li>mysqli_stmt_bind_param($stmt, &apos;s&apos;, </li></ul><ul><li>$ticker); </li></ul><ul><li>mysqli_stmt_bind_result($stmt, $price); </li></ul><ul><li>mysqli_stmt_execute($stmt); </li></ul><ul><li>print &amp;quot;$ticker trades at $price &amp;quot;; </li></ul><ul><li>} </li></ul><ul><li>EBAY trades at 75.01 </li></ul>
  • 10. 4. Bound Parameters <ul><li>$ticker = &apos;EBAY&apos;; </li></ul><ul><li>$stmt = $db-&gt;stmt_init(); </li></ul><ul><li>if ($stmt-&gt;prepare($sql)) { </li></ul><ul><li>$stmt-&gt;bind_param(&apos;s&apos;, $ticker); </li></ul><ul><li>$stmt-&gt;bind_result($price); </li></ul><ul><li>$stmt-&gt;execute(); </li></ul><ul><li>print &amp;quot;$ticker trades at $price &amp;quot;; </li></ul><ul><li>} </li></ul><ul><li>EBAY trades at 75.01 </li></ul>
  • 11. 5. SSL Connections <ul><li>Encrypt the connection between PHP and MySQL </li></ul><ul><li>Slows things down </li></ul><ul><li>Useful when you don’t control the path between the client application and MySQL </li></ul><ul><li>Somewhat painful to set up if you’re an SSL novice </li></ul>
  • 12. 5. SSL Connections <ul><li>$db = mysqli_init(); </li></ul><ul><li>mysqli_ssl_set($db, </li></ul><ul><li>&apos;/usr/local/mysql/server-key.pem&apos;, &apos;/usr/local/mysql/server-cert.pem&apos;, &apos;/usr/local/mysql/cacert.pem&apos;, </li></ul><ul><li>NULL, </li></ul><ul><li>NULL); </li></ul><ul><li>mysqli_real_connect($db, &apos;external.example.org&apos;, &apos;ssl-user&apos;, &apos;password&apos;, &apos;database&apos;); </li></ul>
  • 13. 5. SSL Connections <ul><li>$db = mysqli_init(); </li></ul><ul><li>mysqli_options($db, MYSQLI_READ_DEFAULT_FILE, &apos;/etc/my.cnf&apos;); </li></ul><ul><li>mysqli_real_connect($db, &apos;external.example.org&apos;, &apos;ssl-user&apos;, &apos;password&apos;, &apos;database&apos;); </li></ul>
  • 14. 6. Multi-Query Statements <ul><li>Send multiple SQL queries all at once </li></ul><ul><li>Super useful for phpMyAdmin </li></ul><ul><li>Increases the danger of SQL injection attacks </li></ul><ul><li>Requires special set of functions </li></ul><ul><ul><li>Forcibly disabled in mysqli_query() </li></ul></ul><ul><li>More work to iterate; made easier by using an Iterator ™ </li></ul>
  • 15. 6. Multi-Query Statements <ul><li>if (mysqli_multi_query($db, $query)) { </li></ul><ul><li>do { </li></ul><ul><li>if ($r = mysqli_store_result($db)) { </li></ul><ul><li>while ($row = mysqli_fetch_row($r)) { </li></ul><ul><li>print &amp;quot;$row[0] &amp;quot;; </li></ul><ul><li>} </li></ul><ul><li>mysqli_free_result($result); </li></ul><ul><li>} </li></ul><ul><li>} while (mysqli_next_result($db)); </li></ul><ul><li>} </li></ul>
  • 16. 6. Multi-Query Statements <ul><li>$it = new MySQLiQueryIterator($db, </li></ul><ul><li>$query); </li></ul><ul><li>foreach ($it as $r) { </li></ul><ul><li>if ($r) { </li></ul><ul><li>while ($row = mysqli_fetch_row($r)) { </li></ul><ul><li>print &amp;quot;$row[0] &amp;quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 17. 7. Subselects <ul><li>New in MySQL 4.1 </li></ul><ul><li>Run a query within a query </li></ul><ul><li>Makes it faster and easier to filter data </li></ul><ul><ul><li>Places work inside MySQL instead of PHP </li></ul></ul><ul><li>Could often by “worked around” using a self-join, but not always </li></ul><ul><li>Know what your query will return </li></ul><ul><ul><li>One row ( = ) or many ( IN() )? </li></ul></ul>
  • 18. 7. Subselects <ul><li>mysql&gt; SELECT speaker </li></ul><ul><li>FROM speakers </li></ul><ul><li>WHERE topic = </li></ul><ul><li>(SELECT topic </li></ul><ul><li>FROM speakers </li></ul><ul><li>WHERE speaker = &apos;Adam </li></ul><ul><li>Trachtenberg&apos;); </li></ul><ul><li>Better hope I’m not talking on multiple subjects </li></ul>
  • 19. 7. Subselects <ul><li>mysql&gt; SELECT speaker </li></ul><ul><li>FROM speakers </li></ul><ul><li>WHERE topic IN </li></ul><ul><li>(SELECT topic </li></ul><ul><li>FROM speakers </li></ul><ul><li>WHERE speaker = &apos;Adam </li></ul><ul><li>Trachtenberg&apos;); </li></ul>
  • 20. 8. Character Sets <ul><li>New in MySQL 4.1 </li></ul><ul><li>Store data using different character sets </li></ul><ul><li>Collate data using different character sets </li></ul><ul><li>Important when </li></ul><ul><ul><li>You want a case-insensitive sort </li></ul></ul><ul><ul><li>Different cultures place the same letter in different positions in their alphabets </li></ul></ul><ul><li>What to do with </li></ul><ul><ul><li>n vs ñ </li></ul></ul><ul><ul><li>u vs ü </li></ul></ul>
  • 21. 8. Character Sets <ul><li>Four records </li></ul><ul><ul><li>Muffler </li></ul></ul><ul><ul><li>Müller </li></ul></ul><ul><ul><li>MX Systems </li></ul></ul><ul><ul><li>MySQL </li></ul></ul><ul><li>mysql&gt; SELECT X FROM T ORDER BY X COLLATE collation_name ; </li></ul>MySQL MySQL MySQL MX Systems MX Systems Müller Muffler Müller MX Systems Müller Muffler Muffler latin1_german2_ci latin1_german1_ci latin1_swedish_ci
  • 22. 9. Stored Procedures <ul><li>New in MySQL 5.0 </li></ul><ul><li>Sequence of SQL statements stored on your MySQL server </li></ul><ul><li>Make request with set of parameters, get back chunk of relatively complete data </li></ul><ul><li>Works regardless of client language </li></ul><ul><li>Speedier than even prepared statements </li></ul><ul><li>More secure (can wall off access to tables except through pre-defined procedures) </li></ul><ul><li>A work in progress… </li></ul>
  • 23. 9. Stored Procedures <ul><li>mysql&gt; </li></ul><ul><li>CREATE PRODCEDURE getNumberOfSpeakers </li></ul><ul><li>(OUT n INT) </li></ul><ul><li>BEGIN </li></ul><ul><li>SELECT COUNT(*) INTO n FROM speakers; </li></ul><ul><li>END </li></ul><ul><li>mysql&gt; CALL getNumberOfSpeakers(@number); </li></ul><ul><li>mysql&gt; SELECT @number; </li></ul>
  • 24. 9.1. Cursors <ul><li>New in MySQL 5.0 </li></ul><ul><li>CURrent Set of RecordsS </li></ul><ul><li>Lets you refer to the results of a SELECT statement on the server </li></ul><ul><li>Works within stored procedures and functions </li></ul><ul><li>Still fairly limited. Just a test. </li></ul>
  • 25. 9.1. Cursors <ul><li>mysql&gt; DECLARE speakers CURSOR FOR SELECT speaker, topic FROM speakers; </li></ul><ul><li>mysql&gt; OPEN speakers; </li></ul><ul><li>mysql&gt; FETCH speakers INTO s, t; </li></ul><ul><li>mysql&gt; CLOSE speakers; </li></ul>
  • 26. 10. Views <ul><li>New in MySQL 5.0 </li></ul><ul><li>Let you create a “virtual” table based on SQL queries </li></ul><ul><li>CREATE VIEW view AS SELECT ... </li></ul><ul><li>SELECT statement can include JOIN s </li></ul><ul><li>You can now refer to “view” as if it was a real table: SELECT * FROM view WHERE ... </li></ul><ul><li>Changing rows in the view alters the data back in the original table. </li></ul>
  • 27. 11. Next Year: Go to Eleven <ul><li>NIGEL: What we do is if we need that extra...push over the cliff...you know what we do? </li></ul><ul><li>MARTY: Put it up to eleven. </li></ul><ul><li>NIGEL: Eleven. Exactly. One louder. </li></ul><ul><li>MARTY: Why don&apos;t you just make ten louder and make ten be the top... number...and make that a little louder? </li></ul><ul><li>NIGEL: &lt;looking somewhat befuddled&gt; ...these go to eleven. </li></ul>
  • 28. Shameless Plug: <ul><li>PHP 5, MySQL 4.0, and 4.1 </li></ul><ul><li>New mysqli extension </li></ul><ul><li>Everything covered here, but in greater detail. (Except 5.0) </li></ul><ul><li>How to migrate </li></ul><ul><ul><li>From PHP 4 / mysql / MySQL 4.0 </li></ul></ul><ul><ul><li>To PHP 5 / mysqli / MySQL 4.1 </li></ul></ul>

×