Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP 5 + MySQL 5 = A Perfect 10


Published on

  • Follow the link, new dating source: ❤❤❤ ❤❤❤
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ❶❶❶ ❶❶❶
    Are you sure you want to  Yes  No
    Your message goes here

PHP 5 + MySQL 5 = A Perfect 10

  1. 1. PHP 5 + MySQL 5 A Perfect 10
  2. 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. 3. 1. mysqli extension <ul><li>$db = mysqli_connect($server, $user, $password, &quot;users&quot;); </li></ul><ul><li>$r = mysqli_query($db, &quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = mysqli_fetch_assoc($r)) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>mysqli_free_result($r); </li></ul><ul><li>mysqli_close($db); </li></ul>
  4. 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. 5. 2. Object-Oriented Interface <ul><li>$db = new mysqli($server, $user, $password, &quot;users&quot;); </li></ul><ul><li>$r = $db->query(&quot;SELECT user FROM users&quot;); </li></ul><ul><li>while ($row = $r->fetch_assoc()) { </li></ul><ul><li>print $row['user']; </li></ul><ul><li>} </li></ul><ul><li>$r->free_result(); </li></ul><ul><li>unset($db); </li></ul>
  6. 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. 7. 3. Prepared Statements <ul><li>$db = mysqli_connect($server, $user, $password, 'stocks'); </li></ul><ul><li>$sql = 'SELECT price FROM stocks </li></ul><ul><li>WHERE ticker = ?'; </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. 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. 9. 4. Bound Parameters <ul><li>$ticker = 'EBAY'; </li></ul><ul><li>if (mysqli_stmt_prepare($stmt, $sql)) { </li></ul><ul><li>mysqli_stmt_bind_param($stmt, 's', </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 &quot;$ticker trades at $price &quot;; </li></ul><ul><li>} </li></ul><ul><li>EBAY trades at 75.01 </li></ul>
  10. 10. 4. Bound Parameters <ul><li>$ticker = 'EBAY'; </li></ul><ul><li>$stmt = $db->stmt_init(); </li></ul><ul><li>if ($stmt->prepare($sql)) { </li></ul><ul><li>$stmt->bind_param('s', $ticker); </li></ul><ul><li>$stmt->bind_result($price); </li></ul><ul><li>$stmt->execute(); </li></ul><ul><li>print &quot;$ticker trades at $price &quot;; </li></ul><ul><li>} </li></ul><ul><li>EBAY trades at 75.01 </li></ul>
  11. 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. 12. 5. SSL Connections <ul><li>$db = mysqli_init(); </li></ul><ul><li>mysqli_ssl_set($db, </li></ul><ul><li>'/usr/local/mysql/server-key.pem', '/usr/local/mysql/server-cert.pem', '/usr/local/mysql/cacert.pem', </li></ul><ul><li>NULL, </li></ul><ul><li>NULL); </li></ul><ul><li>mysqli_real_connect($db, '', 'ssl-user', 'password', 'database'); </li></ul>
  13. 13. 5. SSL Connections <ul><li>$db = mysqli_init(); </li></ul><ul><li>mysqli_options($db, MYSQLI_READ_DEFAULT_FILE, '/etc/my.cnf'); </li></ul><ul><li>mysqli_real_connect($db, '', 'ssl-user', 'password', 'database'); </li></ul>
  14. 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. 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 &quot;$row[0] &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. 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 &quot;$row[0] &quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  17. 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. 18. 7. Subselects <ul><li>mysql> 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 = 'Adam </li></ul><ul><li>Trachtenberg'); </li></ul><ul><li>Better hope I’m not talking on multiple subjects </li></ul>
  19. 19. 7. Subselects <ul><li>mysql> 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 = 'Adam </li></ul><ul><li>Trachtenberg'); </li></ul>
  20. 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. 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> 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. 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. 23. 9. Stored Procedures <ul><li>mysql> </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> CALL getNumberOfSpeakers(@number); </li></ul><ul><li>mysql> SELECT @number; </li></ul>
  24. 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. 25. 9.1. Cursors <ul><li>mysql> DECLARE speakers CURSOR FOR SELECT speaker, topic FROM speakers; </li></ul><ul><li>mysql> OPEN speakers; </li></ul><ul><li>mysql> FETCH speakers INTO s, t; </li></ul><ul><li>mysql> CLOSE speakers; </li></ul>
  26. 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. 27. 11. Next Year: Go to Eleven <ul><li>NIGEL: What we do is if we need that extra...push over the 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't you just make ten louder and make ten be the top... number...and make that a little louder? </li></ul><ul><li>NIGEL: <looking somewhat befuddled> ...these go to eleven. </li></ul>
  28. 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>